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

Handle JSON decode error when parsing error response in client #35

Merged
merged 4 commits into from Dec 9, 2021
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion twirp/client.py
@@ -1,3 +1,4 @@
import json
import requests

from . import exceptions
Expand All @@ -22,8 +23,16 @@ def _make_request(self, *args, url, ctx, request, response_obj, **kwargs):
response = response_obj()
response.ParseFromString(resp.content)
return response
else:
try:
raise exceptions.TwirpServerException.from_json(resp.json())
except json.JSONDecodeError:
if resp.status_code == 503:
code = errors.Errors.Unavailable
else:
code = errors.Errors.Unknown
raise exceptions.TwirpServerException(
code=code, message=resp.text
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would it be worth adding the original status code and headers to the meta dict here? And is it ok to assume that resp.text can be sent as message?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For context: the problem I ran into was that an intermediate load balancer was returning a 503 response with an HTML body, but all we were seeing was the JSONDecodeError. We were able to diagnose the actual source of the error only after surfacing the status, headers, and body of the actual response.

Copy link
Contributor

@ofpiyush ofpiyush Nov 21, 2021

Choose a reason for hiding this comment

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

Maybe we should send standard text message and meta.status_code and meta.body

https://twitchtv.github.io/twirp/docs/errors.html#http-errors-from-intermediary-proxies

Check this out!

Copy link
Contributor

Choose a reason for hiding this comment

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

More generally, we should try to follow the go package as closely as possible.

https://github.com/twitchtv/twirp/blob/main/protoc-gen-twirp/generator.go#L635-L664

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Totally agree, I can add to this PR or would that warrant a new one

Copy link
Contributor

Choose a reason for hiding this comment

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

You can add to this one 🙏

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ofpiyush done!

) from None
# Todo: handle error
except requests.exceptions.Timeout as e:
raise exceptions.TwirpServerException(
Expand Down