Skip to content

Commit 596df2e

Browse files
committed
Extended error descriptions
They should provide more information about the origin of the failure.
1 parent 5c6de8a commit 596df2e

1 file changed

Lines changed: 35 additions & 8 deletions

File tree

‎telebot/apihelper.py‎

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,44 @@ def _make_request(token, method_name, method='get', params=None, files=None):
1616
:param method: HTTP method to be used. Defaults to 'get'.
1717
:param params: Optional parameters. Should be a dictionary with key-value pairs.
1818
:param files: Optional files.
19-
:return:
19+
:return: The result parsed to a JSON dictionary.
2020
"""
2121
request_url = telebot.API_URL + 'bot' + token + '/' + method_name
2222
result = requests.request(method, request_url, params=params, files=files)
2323
logger.debug(result.text)
24+
return _check_result(method_name, result)['result']
25+
26+
27+
def _check_result(method_name, result):
28+
"""
29+
Checks whether `result` is a valid API response.
30+
A result is considered invalid if:
31+
- The server returned an HTTP response code other than 200
32+
- The content of the result is invalid JSON.
33+
- The method call was unsuccessful (The JSON 'ok' field equals False)
34+
35+
:raises ApiException: if one of the above listed cases is applicable
36+
:param method_name: The name of the method called
37+
:param result: The returned result of the method request
38+
:return: The result parsed to a JSON dictionary.
39+
"""
2440
if result.status_code != 200:
25-
raise ApiException(method_name, result)
41+
msg = 'The server returned HTTP {0} {1}. Response body:\n[{2}]'\
42+
.format(result.status_code, result.reason, result.text)
43+
raise ApiException(msg, method_name, result)
44+
2645
try:
2746
result_json = result.json()
28-
if not result_json['ok']:
29-
raise Exception()
3047
except:
31-
raise ApiException(method_name, result)
32-
return result_json['result']
48+
msg = 'The server returned an invalid JSON response. Response body:\n[{0}]'\
49+
.format(result.text)
50+
raise ApiException(msg, method_name, result)
51+
52+
if not result_json['ok']:
53+
msg = 'Error code: {0} Description: {1}'\
54+
.format(result_json['error_code'], result_json['description'])
55+
raise ApiException(msg, method_name, result)
56+
return result_json
3357

3458

3559
def get_me(token):
@@ -251,9 +275,12 @@ def split_string(text, chars_per_string):
251275
class ApiException(Exception):
252276
"""
253277
This class represents an Exception thrown when a call to the Telegram API fails.
278+
In addition to an informative message, it has a `function_name` and a `result` attribute, which respectively
279+
contain the name of the failed function and the returned result that made the function to be considered as
280+
failed.
254281
"""
255282

256-
def __init__(self, function_name, result):
257-
super(ApiException, self).__init__('{0} failed. Returned result: {1}'.format(function_name, result))
283+
def __init__(self, msg, function_name, result):
284+
super(ApiException, self).__init__("A request to the Telegram API was unsuccessful. {0}".format(msg))
258285
self.function_name = function_name
259286
self.result = result

0 commit comments

Comments
 (0)