Properly try to parse server message before raising errors

This commit is contained in:
Noam Meltzer 2017-06-21 23:34:35 +03:00 committed by GitHub
parent 6ffd75e421
commit 7a89dcb911

View file

@ -183,6 +183,11 @@ class Request(object):
# 200-299 range are HTTP success statuses
return resp.data
try:
message = self._parse(resp.data)
except ValueError:
message = 'Unknown HTTPError'
if resp.status in (401, 403):
raise Unauthorized(message)
elif resp.status == 400:
@ -190,17 +195,14 @@ class Request(object):
elif resp.status == 404:
raise InvalidToken()
elif resp.status == 413:
raise NetworkError('File too large. Check telegram api limits https://core.telegram.org/bots/api#senddocument')
raise NetworkError('File too large. Check telegram api limits '
'https://core.telegram.org/bots/api#senddocument')
elif resp.status == 502:
raise NetworkError('Bad Gateway')
else:
raise NetworkError('{0} ({1})'.format(message, resp.status))
try:
message = self._parse(resp.data)
except ValueError:
raise NetworkError('Unknown HTTPError {0}'.format(resp.status))
def get(self, url, timeout=None):
"""Request an URL.