Exception classes redesign

Replacing 1 exception class with 3 more specific classes: 
HTTP Exception (server returned http code != 200), 
Invalid JSON Exception (server returned invalid json),
Telegram Expection (telegram returned ok != true)

All 3 classes extend BaseApiException so we can handle all API exceptions at the same time
This commit is contained in:
mrpes 2020-07-31 03:10:34 +05:00 committed by GitHub
parent b790e4e6ba
commit a14424704e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 47 additions and 16 deletions

View File

@ -116,27 +116,22 @@ def _check_result(method_name, result):
- The content of the result is invalid JSON.
- The method call was unsuccessful (The JSON 'ok' field equals False)
:raises ApiException: if one of the above listed cases is applicable
:raises BaseApiException: if one of the above listed cases is applicable
:param method_name: The name of the method called
:param result: The returned result of the method request
:return: The result parsed to a JSON dictionary.
"""
if result.status_code != 200:
msg = 'The server returned HTTP {0} {1}. Response body:\n[{2}]' \
.format(result.status_code, result.reason, result.text.encode('utf8'))
raise ApiException(msg, method_name, result)
raise ApiHTTPException(method_name, result)
try:
result_json = result.json()
except:
msg = 'The server returned an invalid JSON response. Response body:\n[{0}]' \
.format(result.text.encode('utf8'))
raise ApiException(msg, method_name, result)
raise ApiInvalidJSONException(method_name, result)
if not result_json['ok']:
msg = 'Error code: {0} Description: {1}' \
.format(result_json['error_code'], result_json['description'])
raise ApiException(msg, method_name, result)
raise ApiTelegramException(msg, method_name, result, result_json)
return result_json
@ -165,9 +160,8 @@ def download_file(token, file_path):
result = _get_req_session().get(url, proxies=proxy)
if result.status_code != 200:
msg = 'The server returned HTTP {0} {1}. Response body:\n[{2}]' \
.format(result.status_code, result.reason, result.text)
raise ApiException(msg, 'Download file', result)
raise ApiHTTPException('Download file', result)
return result.content
@ -1245,15 +1239,52 @@ def _no_encode(func):
return wrapper
class ApiException(Exception):
class BaseApiException(Exception):
"""
This class represents an Exception thrown when a call to the Telegram API fails.
This class represents a base Exception thrown when a call to the Telegram API fails.
In addition to an informative message, it has a `function_name` and a `result` attribute, which respectively
contain the name of the failed function and the returned result that made the function to be considered as
failed.
"""
def __init__(self, msg, function_name, result):
super(ApiException, self).__init__("A request to the Telegram API was unsuccessful. {0}".format(msg))
super(BaseApiException, self).__init__("A request to the Telegram API was unsuccessful. {0}".format(msg))
self.function_name = function_name
self.result = result
class ApiHTTPException(BaseApiException):
"""
This class represents an Exception thrown when a call to the
Telegram API server returns HTTP code that is not 200.
"""
def __init__(self, function_name, result):
super(ApiHTTPException, self).__init__(
"The server returned HTTP {0} {1}. Response body:\n[{2}]" \
.format(result.status_code, result.reason, result.text.encode('utf8')),
function_name,
result)
class ApiInvalidJSONException(BaseApiException):
"""
This class represents an Exception thrown when a call to the
Telegram API server returns invalid json.
"""
def __init__(self, function_name, result):
super(ApiInvalidJSONException, self).__init__(
"The server returned an invalid JSON response. Response body:\n[{0}]" \
.format(result.text.encode('utf8')),
function_name,
result)
class ApiTelegramException(BaseApiException):
"""
This class represents an Exception thrown when a Telegram API returns error code.
"""
def __init__(self, function_name, result, result_json):
super(ApiTelegramException, self).__init__(
"Error code: {0} Description: {1}" \
.format(result_json['error_code'], result_json['description']),
function_name,
result)
self.result_json = result_json