Skip to content

Commit 7a89dcb

Browse files
authored
Properly try to parse server message before raising errors
1 parent 6ffd75e commit 7a89dcb

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

telegram/utils/request.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,24 +183,26 @@ def _request_wrapper(self, *args, **kwargs):
183183
# 200-299 range are HTTP success statuses
184184
return resp.data
185185

186+
try:
187+
message = self._parse(resp.data)
188+
except ValueError:
189+
message = 'Unknown HTTPError'
190+
186191
if resp.status in (401, 403):
187192
raise Unauthorized(message)
188193
elif resp.status == 400:
189194
raise BadRequest(message)
190195
elif resp.status == 404:
191196
raise InvalidToken()
192197
elif resp.status == 413:
193-
raise NetworkError('File too large. Check telegram api limits https://core.telegram.org/bots/api#senddocument')
198+
raise NetworkError('File too large. Check telegram api limits '
199+
'https://core.telegram.org/bots/api#senddocument')
200+
194201
elif resp.status == 502:
195202
raise NetworkError('Bad Gateway')
196203
else:
197204
raise NetworkError('{0} ({1})'.format(message, resp.status))
198205

199-
try:
200-
message = self._parse(resp.data)
201-
except ValueError:
202-
raise NetworkError('Unknown HTTPError {0}'.format(resp.status))
203-
204206
def get(self, url, timeout=None):
205207
"""Request an URL.
206208

0 commit comments

Comments
 (0)