mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-03-26 08:32:58 +01:00
Introducing telegram.error.BadRequest and testLeaveChat testcase #302
This commit is contained in:
parent
386b91f708
commit
1e398821a0
3 changed files with 19 additions and 6 deletions
|
@ -51,6 +51,7 @@ class TelegramError(Exception):
|
||||||
|
|
||||||
msg = _lstrip_str(message, 'Error: ')
|
msg = _lstrip_str(message, 'Error: ')
|
||||||
msg = _lstrip_str(msg, '[Error]: ')
|
msg = _lstrip_str(msg, '[Error]: ')
|
||||||
|
msg = _lstrip_str(msg, 'Bad Request: ')
|
||||||
if msg != message:
|
if msg != message:
|
||||||
# api_error - capitalize the msg...
|
# api_error - capitalize the msg...
|
||||||
msg = msg.capitalize()
|
msg = msg.capitalize()
|
||||||
|
@ -72,6 +73,10 @@ class InvalidToken(TelegramError):
|
||||||
super(InvalidToken, self).__init__('Invalid token')
|
super(InvalidToken, self).__init__('Invalid token')
|
||||||
|
|
||||||
|
|
||||||
|
class BadRequest(TelegramError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class NetworkError(TelegramError):
|
class NetworkError(TelegramError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
|
@ -28,7 +28,7 @@ from future.moves.urllib.error import HTTPError, URLError
|
||||||
from future.moves.urllib.request import urlopen, urlretrieve, Request
|
from future.moves.urllib.request import urlopen, urlretrieve, Request
|
||||||
|
|
||||||
from telegram import (InputFile, TelegramError)
|
from telegram import (InputFile, TelegramError)
|
||||||
from telegram.error import Unauthorized, NetworkError, TimedOut
|
from telegram.error import Unauthorized, NetworkError, TimedOut, BadRequest
|
||||||
|
|
||||||
|
|
||||||
def _parse(json_data):
|
def _parse(json_data):
|
||||||
|
@ -67,13 +67,15 @@ def _try_except_req(func):
|
||||||
# come first.
|
# come first.
|
||||||
errcode = error.getcode()
|
errcode = error.getcode()
|
||||||
|
|
||||||
if errcode in (401, 403):
|
|
||||||
raise Unauthorized()
|
|
||||||
elif errcode == 502:
|
|
||||||
raise NetworkError('Bad Gateway')
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
message = _parse(error.read())
|
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:
|
except ValueError:
|
||||||
message = 'Unknown HTTPError {0}'.format(error.getcode())
|
message = 'Unknown HTTPError {0}'.format(error.getcode())
|
||||||
|
|
||||||
|
|
|
@ -201,6 +201,12 @@ class BotTest(BaseTest, unittest.TestCase):
|
||||||
|
|
||||||
bot.getMe()
|
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)
|
@flaky(3, 1)
|
||||||
@timeout(10)
|
@timeout(10)
|
||||||
def testGetChat(self):
|
def testGetChat(self):
|
||||||
|
|
Loading…
Add table
Reference in a new issue