-
-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unhandled JSONDecodeError
when Twitter API returns 429
#62
Comments
For the timeout, how much time would be adequate? I'll try to work on the error message. |
Thanks for the quick response. I think a 5~10 seconds timeout would be enough, cause I have 7 service instances running with a 3 seconds time interval between each of them, and no others were reporting 429 when this error was produced. However, I think it would also be OK if it just print some meaningful error message and/or return a custom exit code, cause the command will be ran again by systemd after a set interval. For the custom exit code, according to this guide, the program could use anything in the range other than the reserved ones, or could use C/C++ standards defined in #define EX_UNAVAILABLE 69 /* service unavailable */
#define EX_TEMPFAIL 75 /* temp failure; user is invited to retry */ Though this may only apply to Linux. |
I don't think I'm gonna use a custom exit code because all of python's methods for doing so are pretty hacky (hooking into the exception handling system) Here's the error handling code try:
avatar_content = avatar_content_res.json()
except JSONDecodeError as err:
logging.debug(avatar_content_res.text)
raise LookupError(
(
"Response is incorrect JSON! "
"You probably hit Twitter's ratelimit, retry later."
)
) from err |
Yeah, it can be hacky, and you may have done something different with the exception handling so no worries, but I think it's possible to catch these exceptions in For the error handling code, I think it would be better to check the value of |
That makes sense. Thank you for your input, I'll do that. |
if avatar_content_res.ok:
avatar_content = avatar_content_res.json()
else:
logging.debug(avatar_content_res.text)
if avatar_content_res.status_code == requests.codes.too_many:
raise ValueError(
(
"Response status code is 429! "
"You hit Twitter's ratelimit, retry later."
)
)
if 400 <= avatar_content_res.status_code < 500:
raise ValueError(
"Response code is in the 4XX range. Bad request on our side"
)
if avatar_content_res.status_code >= 500:
raise ValueError(
"Response code is over 500. There was an error on Twitter's side"
)
raise ValueError("Can't get proper response") |
Looks great! Thanks for the quick response and fix! |
alright I'll commit it. I don't think I'll make a release just for it though. |
Thank you so much! I'll build from source so no worries. |
Describe the bug
I have setup a systemd service to monitor the start of Twitter spaces, and the time interval of running the command is 20 seconds. I do have multiple services monitoring multiple accounts, so it's normal to see some
429 Too Many Requests
returned from the Twitter API. However, the response content didn't seem to be valid JSON (likely HTML), and it would cause aJSONDecodeError
to be raised.To Reproduce
Expected behavior
It's not a big issue, but I think it would be better if this can be handled properly. Maybe retry after a timeout, or a better error message?
Output (Note: username and user ID are replaced by placeholders in the following log output)
Desktop (please complete the following information):
The text was updated successfully, but these errors were encountered: