Skip to content

Commit d4beb94

Browse files
committed
Add specific exceptions types for important errors
maintain backward compatibility with the 'old' exception by inherting from TelegramError and using the same message
1 parent 029705e commit d4beb94

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

telegram/bot.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
from telegram import (User, Message, Update, UserProfilePhotos, File,
2727
TelegramError, ReplyMarkup, TelegramObject, NullHandler)
28+
from telegram.error import InvalidToken
2829
from telegram.utils import request
2930

3031
H = NullHandler()
@@ -751,5 +752,5 @@ def _valid_token(token):
751752
"""a very basic validation on token"""
752753
left, sep, _right = token.partition(':')
753754
if (not sep) or (not left.isdigit()) or (len(left) < 3):
754-
raise TelegramError('Invalid token')
755+
raise InvalidToken()
755756
return token

telegram/error.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,25 @@ def __init__(self, message):
5959

6060
def __str__(self):
6161
return '%s' % (self.message)
62+
63+
64+
class Unauthorized(TelegramError):
65+
66+
def __init__(self):
67+
super(Unauthorized, self).__init__('Unauthorized')
68+
69+
70+
class InvalidToken(TelegramError):
71+
72+
def __init__(self):
73+
super(InvalidToken, self).__init__('Invalid token')
74+
75+
76+
class NetworkError(TelegramError):
77+
pass
78+
79+
80+
class TimedOut(NetworkError):
81+
82+
def __init__(self):
83+
super(TimedOut, self).__init__('Timed out')

telegram/utils/request.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
from urllib2 import HTTPError
4444

4545
from telegram import (InputFile, TelegramError)
46+
from telegram.error import Unauthorized, NetworkError, TimedOut
4647

4748

4849
def _parse(json_data):
@@ -79,7 +80,7 @@ def decorator(*args, **kwargs):
7980
# `HTTPError` inherits from `URLError` so `HTTPError` handling must
8081
# come first.
8182
if error.getcode() == 403:
82-
raise TelegramError('Unauthorized')
83+
raise Unauthorized()
8384
if error.getcode() == 502:
8485
raise TelegramError('Bad Gateway')
8586

@@ -88,19 +89,20 @@ def decorator(*args, **kwargs):
8889
except ValueError:
8990
message = 'Unknown HTTPError {0}'.format(error.getcode())
9091

91-
raise TelegramError(message)
92+
raise NetworkError(message)
9293

9394
except URLError as error:
94-
raise TelegramError('URLError: {0!r}'.format(error))
95+
raise NetworkError('URLError: {0!r}'.format(error))
9596

9697
except (SSLError, socket.timeout) as error:
97-
if "operation timed out" in str(error):
98-
raise TelegramError("Timed out")
98+
err_s = str(error)
99+
if "operation timed out" in err_s:
100+
raise TimedOut()
99101

100-
raise TelegramError(str(error))
102+
raise NetworkError(err_s)
101103

102104
except HTTPException as error:
103-
raise TelegramError('HTTPException: {0!r}'.format(error))
105+
raise NetworkError('HTTPException: {0!r}'.format(error))
104106

105107
return decorator
106108

tests/test_bot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def testGetUserProfilePhotos(self):
145145

146146
def _test_invalid_token(self, token):
147147
print('Testing invalid token: {0}'.format(token))
148-
self.assertRaisesRegexp(telegram.TelegramError, 'Invalid token', telegram.Bot, token)
148+
self.assertRaisesRegexp(telegram.error.InvalidToken, 'Invalid token', telegram.Bot, token)
149149

150150
def testInvalidToken1(self):
151151
self._test_invalid_token('123')
@@ -158,7 +158,7 @@ def testInvalidToken3(self):
158158

159159
def testUnauthToken(self):
160160
print('Testing unauthorized token')
161-
with self.assertRaisesRegexp(telegram.TelegramError, 'Unauthorized'):
161+
with self.assertRaisesRegexp(telegram.error.Unauthorized, 'Unauthorized'):
162162
bot = telegram.Bot('1234:abcd1234')
163163
bot.getMe()
164164

0 commit comments

Comments
 (0)