Skip to content
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

better handler API error parsing. #1510

Merged
merged 1 commit into from
Sep 13, 2024

Conversation

lavigne958
Copy link
Collaborator

Possibly we encountered an error while parsing the API error JSON, like connection closes early etc and the received JSON is invalid.

Better handle such scenario and build a valid error Dict to keep the exception stack flow running and raise this message to the application.

extract the returned text so if we any bit of valid information at least we can pass that to the application.

This could be helpfull to if we add calls to the API made for web browser that returns HTML error message, it will fail to decode the recieved error and raise a proper error.

closes #1504

Possibly we encountered an error while parsing the API error JSON, like
connection closes early etc and the received JSON is invalid.

Better handle such scenario and build a valid error Dict to keep the
exception stack flow running and raise this message to the application.

extract the returned text so if we any bit of valid information at least
we can pass that to the application.

This could be helpfull to if we add calls to the API made for web
browser that returns HTML error message, it will fail to decode the
recieved error and raise a proper error.

closes #1504

Signed-off-by: Alexandre Lavigne <[email protected]>
@lavigne958 lavigne958 requested a review from alifeee September 8, 2024 16:55
@lavigne958 lavigne958 self-assigned this Sep 8, 2024
Copy link
Collaborator

@alifeee alifeee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me.

is there a way to test this?

@lavigne958
Copy link
Collaborator Author

I could possibly manually test it, but that's quiet hard to do. otherwise, using automatic test no 😞
that would require us to close the connection perfectly in the middle of the transfer of the response from the server, way too hard to be automatic 😆

we do not regress in anything so far.

@lavigne958 lavigne958 merged commit 6973eef into master Sep 13, 2024
10 checks passed
@lavigne958 lavigne958 deleted the bugfix/handler_errors_in_api_error_parsing branch September 13, 2024 17:14
@alifeee
Copy link
Collaborator

alifeee commented Sep 14, 2024

we could make a test that uses a HTTP client and mock the response?

@lavigne958
Copy link
Collaborator Author

lavigne958 commented Sep 14, 2024

we could make a test that uses a HTTP client and mock the response?

Possibly yes 🤔
We could use a custom http client for a single request that returns an invalid JSON.

I'm worried how this is gonna play with the cassette recorder too 🤔 it's worth the try.

@dhuang
Copy link

dhuang commented Sep 20, 2024

appreciate the quick fix here, we started seeing these as well. any chance we'll get a release with this change?

@lavigne958
Copy link
Collaborator Author

It's done ✔️
in #1512 I managed to create a fake HTTP client that can be tested and raises the appropriate exception (the regular APIError exception).

@lavigne958
Copy link
Collaborator Author

appreciate the quick fix here, we started seeing these as well. any chance we'll get a release with this change?

so far we can't tell, we have quite a few new commits but nothing minor/major except this one.
we need to discuss what is waiting to be reviewed or merged and check our priorities.

@natebessa
Copy link

natebessa commented Sep 24, 2024

Helpful change, thanks for fixing. We need this fix because suddenly we've been getting a lot of 502 errors from the Google Sheets API. The response on the 502 does not contain a json object but instead the HTML/CSS of an error page, which triggers a JSONDecodeError. We implemented gspreads BackOffHTTPClient to retry on those 502s but the JSONDecodeError breaks that.

Will be on the look out for a release. Will be massively helpful. Thanks.

@alifeee alifeee mentioned this pull request Sep 24, 2024
@alifeee
Copy link
Collaborator

alifeee commented Sep 24, 2024

I have turned the release into an issue... we will do it soon :)

#1516

# and keep the exception raise flow running

error = {
"code": -1,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @lavigne958 I'm wondering if the error code here should instead be response.status_code. I noticed this because the BackOffHTTPClient will not retry to send the request because -1 is not an expected error code here.

Copy link
Collaborator Author

@lavigne958 lavigne958 Sep 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi that's a good point.

Unfortunately we can't simply set the return code as we failed to parse the returned error.

This could be 2 reasons: 1. The response is incomplete/connection lost etc. 2. The response is in html or some other format we don't handle nor expect but the response is valid !

I will check with alifee what is best here, I have 2 ideas:

  1. Instead of returning a mock http response, we raise a new Gspread issue and the user is in charge of retrying or not. We don't take a decision here.

  2. We add a new setting for the http client to retry on -1 errors. So the user can enable that setting and the http client will retry on -1 error code too.

@alifeee what you do think ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For what it's worth, I imagine there would still be HTTP codes coming back with the scenarios you mentioned, e.g. 408 (timeout), 502 (server error, with html response).

@Kirkman
Copy link

Kirkman commented Oct 1, 2024

Helpful change, thanks for fixing. We need this fix because suddenly we've been getting a lot of 502 errors from the Google Sheets API. The response on the 502 does not contain a json object but instead the HTML/CSS of an error page, which triggers a JSONDecodeError. We implemented gspreads BackOffHTTPClient to retry on those 502s but the JSONDecodeError breaks that.

Glad to know I'm not the only one seeing this the last few weeks.

@natebessa
Copy link

natebessa commented Oct 1, 2024

@Kirkman absolutely. It's almost 1 in 10 calls for me at this rate. Not something I'd expect from Google, even if it's a free service. I can't find much chatter about it online, and no acknowledgement from Google on any social media. Meanwhile my Google Cloud dashboard is massively undercounting the API error rate I'm seeing.

But I've since forked the repo, added the status_code fix I mention above, implemented the BackOffHttpClient, and have been swimming problem-free since.

@eitchtee
Copy link

eitchtee commented Oct 3, 2024

The intermittent 502 errors seems to be a bug on Google's end, related issue: https://issuetracker.google.com/issues/369670473

@alifeee alifeee mentioned this pull request Oct 3, 2024
@alifeee
Copy link
Collaborator

alifeee commented Oct 3, 2024

hi all

a new release 6.1.3 has been released (on github) (on PyPi) which contains a fix for this issue

we now catch the non-JSON error and turn it into a GSpreadException which should be easier to catch and find out what went wrong with the request

see more context here: #1510

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Exception requests.exceptions.JSONDecodeError is raised in APIError constructor
6 participants