From 1c0474312806a4f3315f704c955da7f96a9730a6 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 28 Mar 2017 16:49:58 +0200 Subject: [PATCH] Edited TelegramAPIError class --- telegram.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) 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"]