Skip to content

Request builder treats all non-success status codes as JSON #595

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

Open
skogler opened this issue May 14, 2025 · 2 comments
Open

Request builder treats all non-success status codes as JSON #595

skogler opened this issue May 14, 2025 · 2 comments
Labels
bug Something isn't working

Comments

@skogler
Copy link

skogler commented May 14, 2025

The error handling in at least the request builder here is not handling non-JSON errors:

raise APIError(r.json())

For non-JSON content, this will trigger AttributeError: 'str' object has no attribute 'get' in the APIError's __init__

The issue in our case is that sometimes network connection failures happen, in which case the response content is:

gateway error: Error: Network connection lost.

I suspect this is coming from the Supabase API gateway. Nevertheless, I think the client could handle this more gracefully by checking the content-type of the response and raising a wrapped error like in the handler for the JSONDecodeError two lines below.

@o-santi
Copy link

o-santi commented May 16, 2025

I think I found a way to reproduce this, as well as the root cause. In the case of a non-successful return code, the request builder will try to raise an APIError(r.json()), and it correctly handles the case where the response may not be converted to JSON, by catching JSONDecodeError in the try block.

It does not, however, handle the case where the r.json()'s return type is not a Dict[str, str], as .json() may return an integer, a float, a string, a list, or any recursive combination of those. The most common case, and I believe may be what you experienced, is when the text field of the response comes surrounded by quotes, making the response a valid JSON that's converted to a str type, which when passed to the APIError.__init__ function causes an error.

I've opened #597 to handle this gracefully.

@skogler
Copy link
Author

skogler commented May 16, 2025

Yes, that is exactly the issue I had. I patched this locally, but would be happy if this was fixed upstream.

What would be nice is if the message that is returned would be the message field of the APIError if it is a string, instead of being put into the details field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants