Add specific exceptions types for important errors

maintain backward compatibility with the 'old' exception by inherting
from TelegramError and using the same message
This commit is contained in:
Noam Meltzer 2016-01-24 00:26:27 +02:00
parent 029705e0af
commit d4beb94059
4 changed files with 35 additions and 10 deletions

View file

@ -25,6 +25,7 @@ import logging
from telegram import (User, Message, Update, UserProfilePhotos, File,
TelegramError, ReplyMarkup, TelegramObject, NullHandler)
from telegram.error import InvalidToken
from telegram.utils import request
H = NullHandler()
@ -751,5 +752,5 @@ class Bot(TelegramObject):
"""a very basic validation on token"""
left, sep, _right = token.partition(':')
if (not sep) or (not left.isdigit()) or (len(left) < 3):
raise TelegramError('Invalid token')
raise InvalidToken()
return token

View file

@ -59,3 +59,25 @@ class TelegramError(Exception):
def __str__(self):
return '%s' % (self.message)
class Unauthorized(TelegramError):
def __init__(self):
super(Unauthorized, self).__init__('Unauthorized')
class InvalidToken(TelegramError):
def __init__(self):
super(InvalidToken, self).__init__('Invalid token')
class NetworkError(TelegramError):
pass
class TimedOut(NetworkError):
def __init__(self):
super(TimedOut, self).__init__('Timed out')

View file

@ -43,6 +43,7 @@ except ImportError:
from urllib2 import HTTPError
from telegram import (InputFile, TelegramError)
from telegram.error import Unauthorized, NetworkError, TimedOut
def _parse(json_data):
@ -79,7 +80,7 @@ def _try_except_req(func):
# `HTTPError` inherits from `URLError` so `HTTPError` handling must
# come first.
if error.getcode() == 403:
raise TelegramError('Unauthorized')
raise Unauthorized()
if error.getcode() == 502:
raise TelegramError('Bad Gateway')
@ -88,19 +89,20 @@ def _try_except_req(func):
except ValueError:
message = 'Unknown HTTPError {0}'.format(error.getcode())
raise TelegramError(message)
raise NetworkError(message)
except URLError as error:
raise TelegramError('URLError: {0!r}'.format(error))
raise NetworkError('URLError: {0!r}'.format(error))
except (SSLError, socket.timeout) as error:
if "operation timed out" in str(error):
raise TelegramError("Timed out")
err_s = str(error)
if "operation timed out" in err_s:
raise TimedOut()
raise TelegramError(str(error))
raise NetworkError(err_s)
except HTTPException as error:
raise TelegramError('HTTPException: {0!r}'.format(error))
raise NetworkError('HTTPException: {0!r}'.format(error))
return decorator

View file

@ -145,7 +145,7 @@ class BotTest(BaseTest, unittest.TestCase):
def _test_invalid_token(self, token):
print('Testing invalid token: {0}'.format(token))
self.assertRaisesRegexp(telegram.TelegramError, 'Invalid token', telegram.Bot, token)
self.assertRaisesRegexp(telegram.error.InvalidToken, 'Invalid token', telegram.Bot, token)
def testInvalidToken1(self):
self._test_invalid_token('123')
@ -158,7 +158,7 @@ class BotTest(BaseTest, unittest.TestCase):
def testUnauthToken(self):
print('Testing unauthorized token')
with self.assertRaisesRegexp(telegram.TelegramError, 'Unauthorized'):
with self.assertRaisesRegexp(telegram.error.Unauthorized, 'Unauthorized'):
bot = telegram.Bot('1234:abcd1234')
bot.getMe()