Enabling disk_cache can lead to an exception (invalid_index) when changing channel #25

Closed
opened 2023-08-05 18:14:17 +02:00 by jynus · 4 comments
jynus commented 2023-08-05 18:14:17 +02:00 (Migrated from github.com)

I did the following:

  • Enabled disk_cache
  • Connected and read some events from one Twitch channel
  • Disconnected/closed my application
  • Connected to a different channel

I got the following error:

Invalid get index 'badge_sets' (on base 'Dictionary'). (gift_node.gd:525)
Screenshot_20230805_175123

I guessed that it assumes that if the cache exists, it contains this key. I fixed the issue by changing the logic to this:

		if (disk_cache && FileAccess.file_exists(filename)):
			var cache = JSON.parse_string(FileAccess.get_file_as_string(filename))
			if "badge_sets" in cache:
				return cache["badge_sets"]

		var request : HTTPRequest = HTTPRequest.new()
		add_child(request)
		request.request("https://api.twitch.tv/helix/chat/badges" + ("/global" if channel_id == "_global" else "?broadcaster_id=" + channel_id), [USER_AGENT, "Authorization: Bearer " + token["access_token"], "Client-Id:" + client_id, "Content-Type: application/json"], HTTPClient.METHOD_GET)

Let me know what you think and I can send you a PR, if it seems like a reasonable change.

I did the following: * Enabled disk_cache * Connected and read some events from one Twitch channel * Disconnected/closed my application * Connected to a different channel I got the following error: `Invalid get index 'badge_sets' (on base 'Dictionary').` (gift_node.gd:525) ![Screenshot_20230805_175123](https://github.com/issork/gift/assets/8576860/6f1e231c-3d6c-437c-a39d-745b14ea08a5) I guessed that it assumes that if the cache exists, it contains this key. I fixed the issue by changing the logic to this: ``` if (disk_cache && FileAccess.file_exists(filename)): var cache = JSON.parse_string(FileAccess.get_file_as_string(filename)) if "badge_sets" in cache: return cache["badge_sets"] var request : HTTPRequest = HTTPRequest.new() add_child(request) request.request("https://api.twitch.tv/helix/chat/badges" + ("/global" if channel_id == "_global" else "?broadcaster_id=" + channel_id), [USER_AGENT, "Authorization: Bearer " + token["access_token"], "Client-Id:" + client_id, "Content-Type: application/json"], HTTPClient.METHOD_GET) ``` Let me know what you think and I can send you a PR, if it seems like a reasonable change.
issork commented 2023-08-10 13:03:17 +02:00 (Migrated from github.com)

Hey, sorry for the late response - the change looks good to me, feel free to create a PR ^^

Hey, sorry for the late response - the change looks good to me, feel free to create a PR ^^
jynus commented 2023-08-11 14:06:12 +02:00 (Migrated from github.com)

Will do.

Will do.
jynus commented 2023-08-19 13:39:36 +02:00 (Migrated from github.com)

There you have it- I chose to short circuit the 3 main blocks, as I thought it was clearer in this case than the "single return at the end". I tested it by connecting and reconnecting to several channels and deleting the cache files several times, but further testing will be welcome, too! :-D

There you have it- I chose to short circuit the 3 main blocks, as I thought it was clearer in this case than the "single return at the end". I tested it by connecting and reconnecting to several channels and deleting the cache files several times, but further testing will be welcome, too! :-D
jynus commented 2023-09-09 11:11:20 +02:00 (Migrated from github.com)

Thank you a lot!

Thank you a lot!
Sign in to join this conversation.
No description provided.