From a2fddbe85cc81f3f780eb3971fce77350e255b5d Mon Sep 17 00:00:00 2001 From: lisitsky Date: Wed, 9 Nov 2016 16:36:42 +0300 Subject: [PATCH] Fix telegram API change, returning '404 Not found' (#461) * Fix telegram API change, returning '404 Not found' with raising own TelegramError rather native exception * Change exception to InvalidToken in test and request util * Added myself to AUTHORS. Thx for appreciation :) --- AUTHORS.rst | 1 + telegram/utils/request.py | 6 ++++-- tests/test_bot.py | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/AUTHORS.rst b/AUTHORS.rst index c146e5be1..9c33bc522 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -13,6 +13,7 @@ The following wonderful people contributed directly or indirectly to this projec - `bimmlerd `_ - `Eli Gao `_ - `ErgoZ Riftbit Vaper `_ +- `Eugene Lisitsky `_ - `franciscod `_ - `Jacob Bom `_ - `JASON0916 `_ diff --git a/telegram/utils/request.py b/telegram/utils/request.py index 0aa071c84..85f82becf 100644 --- a/telegram/utils/request.py +++ b/telegram/utils/request.py @@ -31,8 +31,8 @@ import urllib3 from urllib3.connection import HTTPConnection from telegram import (InputFile, TelegramError) -from telegram.error import Unauthorized, NetworkError, TimedOut, BadRequest, ChatMigrated, \ - RetryAfter +from telegram.error import Unauthorized, InvalidToken, NetworkError, TimedOut, BadRequest, \ + ChatMigrated, RetryAfter logging.getLogger('urllib3').setLevel(logging.WARNING) @@ -150,6 +150,8 @@ class Request(object): raise Unauthorized() elif resp.status == 400: raise BadRequest(repr(message)) + elif resp.status == 404: + raise InvalidToken() elif resp.status == 502: raise NetworkError('Bad Gateway') else: diff --git a/tests/test_bot.py b/tests/test_bot.py index e598c4ded..ed7d37c09 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -227,7 +227,7 @@ class BotTest(BaseTest, unittest.TestCase): bot.getMe() def testInvalidSrvResp(self): - with self.assertRaisesRegexp(telegram.TelegramError, 'Invalid server response'): + with self.assertRaisesRegexp(telegram.error.InvalidToken, 'Invalid token'): # bypass the valid token check newbot_cls = type( 'NoTokenValidateBot', (telegram.Bot,), dict(_validate_token=lambda x, y: None))