From c17a2379ba014d5c87352c9c0f2d48300f7f7398 Mon Sep 17 00:00:00 2001 From: "Mr. Dog" <68982655+mrpes@users.noreply.github.com> Date: Mon, 3 Aug 2020 06:39:12 +0500 Subject: [PATCH 1/2] Exceptions classes redesign followup --- telebot/apihelper.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/telebot/apihelper.py b/telebot/apihelper.py index 7603d17..3f3ddfd 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -105,7 +105,10 @@ def _make_request(token, method_name, method='get', params=None, files=None): timeout=(connect_timeout, read_timeout), proxies=proxy) logger.debug("The server returned: '{0}'".format(result.text.encode('utf8'))) - return _check_result(method_name, result)['result'] + + json_result = _check_result(method_name, result) + if json_result: + return json_result['result'] def _check_result(method_name, result): @@ -121,18 +124,18 @@ def _check_result(method_name, result): :param result: The returned result of the method request :return: The result parsed to a JSON dictionary. """ - if result.status_code != 200: - raise ApiHTTPException(method_name, result) - try: result_json = result.json() except: raise ApiInvalidJSONException(method_name, result) - - if not result_json['ok']: - raise ApiTelegramException(msg, method_name, result, result_json) - - return result_json + else: + if not result_json['ok']: + raise ApiTelegramException(method_name, result, result_json) + + elif result.status_code != 200: + raise ApiHTTPException(method_name, result) + + return result_json def get_me(token): @@ -1291,4 +1294,5 @@ class ApiTelegramException(ApiException): function_name, result) self.result_json = result_json + self.error_code = result_json['error_code'] From 1bb98483c282591766fcfccf04ec85749e4db7cc Mon Sep 17 00:00:00 2001 From: "Mr. Dog" <68982655+mrpes@users.noreply.github.com> Date: Tue, 4 Aug 2020 05:34:13 +0500 Subject: [PATCH 2/2] Update apihelper.py --- telebot/apihelper.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/telebot/apihelper.py b/telebot/apihelper.py index 3f3ddfd..964104a 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -127,14 +127,15 @@ def _check_result(method_name, result): try: result_json = result.json() except: - raise ApiInvalidJSONException(method_name, result) + if result.status_code != 200: + raise ApiHTTPException(method_name, result) + else: + raise ApiInvalidJSONException(method_name, result) + else: if not result_json['ok']: raise ApiTelegramException(method_name, result, result_json) - elif result.status_code != 200: - raise ApiHTTPException(method_name, result) - return result_json