diff --git a/telegram.py b/telegram.py index 930b0088..d51b5085 100644 --- a/telegram.py +++ b/telegram.py @@ -5,7 +5,11 @@ import datetime class TelegramAPIError(Exception): - pass + def __init__(self, code, description): + # Error code + self.code = code + # Error description + self.description = description class UpdateError(Exception): @@ -142,14 +146,8 @@ class Bot: # Parse the json data as soon it's ready data = await response.json() # Check for errors in the request - if "description" in data: - error = data["description"] - if response.status != 200: - raise TelegramAPIError(f"Request returned {response.status} {response.reason}") - # Check for errors in the response - if not data["ok"]: - error = data["description"] - raise TelegramAPIError(f"Response returned an error: {error}") + if response.status != 200 or not data["ok"]: + raise TelegramAPIError(data["error_code"], data["description"]) # Return a dictionary containing the data return data["result"]