Introducing telegram.error.BadRequest and testLeaveChat testcase #302

This commit is contained in:
Leandro Toledo 2016-05-25 21:15:17 -03:00
parent 386b91f708
commit 1e398821a0
3 changed files with 19 additions and 6 deletions

View file

@ -51,6 +51,7 @@ class TelegramError(Exception):
msg = _lstrip_str(message, 'Error: ')
msg = _lstrip_str(msg, '[Error]: ')
msg = _lstrip_str(msg, 'Bad Request: ')
if msg != message:
# api_error - capitalize the msg...
msg = msg.capitalize()
@ -72,6 +73,10 @@ class InvalidToken(TelegramError):
super(InvalidToken, self).__init__('Invalid token')
class BadRequest(TelegramError):
pass
class NetworkError(TelegramError):
pass

View file

@ -28,7 +28,7 @@ from future.moves.urllib.error import HTTPError, URLError
from future.moves.urllib.request import urlopen, urlretrieve, Request
from telegram import (InputFile, TelegramError)
from telegram.error import Unauthorized, NetworkError, TimedOut
from telegram.error import Unauthorized, NetworkError, TimedOut, BadRequest
def _parse(json_data):
@ -67,13 +67,15 @@ def _try_except_req(func):
# come first.
errcode = error.getcode()
if errcode in (401, 403):
raise Unauthorized()
elif errcode == 502:
raise NetworkError('Bad Gateway')
try:
message = _parse(error.read())
if errcode in (401, 403):
raise Unauthorized()
elif errcode == 400:
raise BadRequest(message)
elif errcode == 502:
raise NetworkError('Bad Gateway')
except ValueError:
message = 'Unknown HTTPError {0}'.format(error.getcode())

View file

@ -201,6 +201,12 @@ class BotTest(BaseTest, unittest.TestCase):
bot.getMe()
@flaky(3, 1)
@timeout(10)
def testLeaveChat(self):
with self.assertRaisesRegexp(telegram.error.BadRequest, 'Chat not found'):
chat = self._bot.leaveChat(-123456)
@flaky(3, 1)
@timeout(10)
def testGetChat(self):