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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion twirp/client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import requests

from . import exceptions
Expand All @@ -22,8 +23,10 @@ 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:
raise self._twirp_error_from_intermediary(resp) from None
# Todo: handle error
except requests.exceptions.Timeout as e:
raise exceptions.TwirpServerException(
Expand All @@ -37,3 +40,42 @@ def _make_request(self, *args, url, ctx, request, response_obj, **kwargs):
message=str(e),
meta={"original_exception": e},
)

@staticmethod
def _twirp_error_from_intermediary(resp):
# see https://twitchtv.github.io/twirp/docs/errors.html#http-errors-from-intermediary-proxies
meta = {
'http_error_from_intermediary': 'true',
'status_code': str(resp.status_code),
}

if resp.is_redirect:
# twirp uses POST which should not redirect
code = errors.Errors.Internal
location = resp.headers.get('location')
message = 'unexpected HTTP status code %d "%s" received, Location="%s"' % (
resp.status_code,
resp.reason,
location,
)
meta['location'] = location

else:
code = {
400: errors.Errors.Internal, # JSON response should have been returned
401: errors.Errors.Unauthenticated,
403: errors.Errors.PermissionDenied,
404: errors.Errors.BadRoute,
429: errors.Errors.ResourceExhausted,
502: errors.Errors.Unavailable,
503: errors.Errors.Unavailable,
504: errors.Errors.Unavailable,
}.get(resp.status_code, errors.Errors.Unknown)

message = 'Error from intermediary with HTTP status code %d "%s"' % (
resp.status_code,
resp.reason,
)
meta['body'] = resp.text

return exceptions.TwirpServerException(code=code, message=message, meta=meta)