Skip to content

Commit a2d8ca3

Browse files
committed
some errors are prefixed with '[Error]: ' - strip that down
use custom _lstrip_str() func instead of regex. a little performance improvment and (IMO) a bit more readable.
1 parent 97ebebe commit a2d8ca3

1 file changed

Lines changed: 26 additions & 7 deletions

File tree

telegram/error.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,43 @@
1818

1919
"""This module contains a object that represents a Telegram Error"""
2020

21-
import re
21+
22+
def _lstrip_str(in_s, lstr):
23+
"""
24+
Args:
25+
in_s (str): in string
26+
lstr (str): substr to strip from left side
27+
28+
Returns:
29+
str:
30+
31+
"""
32+
if in_s.startswith(lstr):
33+
res = in_s[len(lstr):]
34+
else:
35+
res = in_s
36+
return res
2237

2338

2439
class TelegramError(Exception):
2540
"""This object represents a Telegram Error."""
2641

2742
def __init__(self, message):
2843
"""
44+
Args:
45+
message (str):
46+
2947
Returns:
30-
str:
48+
3149
"""
3250
super(TelegramError, self).__init__()
3351

34-
api_error = re.match(r'^Error: (?P<message>.*)', message)
35-
if api_error:
36-
self.message = api_error.group('message').capitalize()
37-
else:
38-
self.message = message
52+
msg = _lstrip_str(message, 'Error: ')
53+
msg = _lstrip_str(msg, '[Error]: ')
54+
if msg != message:
55+
# api_error - capitalize the msg...
56+
msg = msg.capitalize()
57+
self.message = msg
3958

4059
def __str__(self):
4160
return '%s' % (self.message)

0 commit comments

Comments
 (0)