aiogram/aiogram/utils/exceptions.py

263 lines
6.4 KiB
Python
Raw Normal View History

2018-03-30 19:32:23 +03:00
"""
TelegramAPIError
ValidationError
Throttled
BadRequest
MessageError
MessageNotModified
MessageToForwardNotFound
MessageToDeleteNotFound
MessageIdentifierNotSpecified
ChatNotFound
InvalidQueryID
InvalidPeerID
InvalidHTTPUrlContent
WrongFileIdentifier
GroupDeactivated
BadWebhook
WebhookRequireHTTPS
BadWebhookPort
CantParseUrl
NotFound
MethodNotKnown
2018-04-08 18:16:52 +03:00
PhotoAsInputFileRequired
2018-04-14 02:40:16 +03:00
ToMuchMessages
2018-04-16 00:36:14 +03:00
InvalidStickersSet
2018-04-22 22:02:58 +03:00
ChatAdminRequired
2018-03-30 19:32:23 +03:00
ConflictError
TerminatedByOtherGetUpdates
CantGetUpdates
Unauthorized
BotKicked
BotBlocked
UserDeactivated
2018-04-14 02:40:16 +03:00
CantInitiateConversation
2018-03-30 19:32:23 +03:00
NetworkError
RetryAfter
MigrateToChat
AIOGramWarning
TimeoutWarning
"""
2017-12-02 01:03:56 +02:00
import time
2018-03-30 19:32:23 +03:00
_PREFIXES = ['Error: ', '[Error]: ', 'Bad Request: ', 'Conflict: ', 'Not Found: ']
2017-08-11 05:58:27 +03:00
def _clean_message(text):
for prefix in _PREFIXES:
if text.startswith(prefix):
text = text[len(prefix):]
2017-08-22 20:28:22 +03:00
return (text[0].upper() + text[1:]).strip()
2017-07-25 02:52:20 +03:00
2017-08-11 05:58:27 +03:00
class TelegramAPIError(Exception):
2018-03-30 19:32:23 +03:00
def __init__(self, message=None):
2017-08-11 05:58:27 +03:00
super(TelegramAPIError, self).__init__(_clean_message(message))
2018-03-30 19:32:23 +03:00
class _MatchErrorMixin:
match = ''
text = None
@classmethod
def check(cls, message):
return cls.match in message
@classmethod
def throw(cls):
raise cls(cls.text or cls.match)
2017-08-23 22:23:57 +03:00
class AIOGramWarning(Warning):
pass
class TimeoutWarning(AIOGramWarning):
pass
2018-03-30 19:32:23 +03:00
class FSMStorageWarning(AIOGramWarning):
pass
2017-08-11 05:58:27 +03:00
class ValidationError(TelegramAPIError):
2017-05-19 21:20:59 +03:00
pass
2017-08-11 05:58:27 +03:00
class BadRequest(TelegramAPIError):
pass
2018-03-30 19:32:23 +03:00
class MessageError(BadRequest):
pass
class MessageNotModified(MessageError, _MatchErrorMixin):
2018-04-08 18:16:52 +03:00
"""
Will be raised when you try to set new text is equals to current text.
"""
2018-03-30 19:32:23 +03:00
match = 'message is not modified'
class MessageToForwardNotFound(MessageError, _MatchErrorMixin):
2018-04-08 18:16:52 +03:00
"""
Will be raised when you try to forward very old or deleted or unknown message.
"""
2018-03-30 19:32:23 +03:00
match = 'message to forward not found'
class MessageToDeleteNotFound(MessageError, _MatchErrorMixin):
2018-04-08 18:16:52 +03:00
"""
Will be raised when you try to delete very old or deleted or unknown message.
"""
2018-03-30 19:32:23 +03:00
match = 'message to delete not found'
class MessageIdentifierNotSpecified(MessageError, _MatchErrorMixin):
match = 'message identifier is not specified'
class ChatNotFound(BadRequest, _MatchErrorMixin):
match = 'chat not found'
class InvalidQueryID(BadRequest, _MatchErrorMixin):
match = 'QUERY_ID_INVALID'
text = 'Invalid query ID'
class InvalidPeerID(BadRequest, _MatchErrorMixin):
match = 'PEER_ID_INVALID'
text = 'Invalid peer ID'
class InvalidHTTPUrlContent(BadRequest, _MatchErrorMixin):
match = 'Failed to get HTTP URL content'
class WrongFileIdentifier(BadRequest, _MatchErrorMixin):
match = 'wrong file identifier/HTTP URL specified'
class GroupDeactivated(BadRequest, _MatchErrorMixin):
match = 'group is deactivated'
class PhotoAsInputFileRequired(BadRequest, _MatchErrorMixin):
2018-04-08 18:16:52 +03:00
"""
Will be raised when you try to set chat photo from file ID.
"""
match = 'Photo should be uploaded as an InputFile'
2018-04-14 02:40:16 +03:00
class ToMuchMessages(BadRequest, _MatchErrorMixin):
"""
Will be raised when you try to send media group with more than 10 items.
"""
match = 'Too much messages to send as an album'
2018-04-16 00:36:14 +03:00
class InvalidStickersSet(BadRequest, _MatchErrorMixin):
match = 'STICKERSET_INVALID'
text = 'Stickers set is invalid'
2018-04-22 22:02:58 +03:00
class ChatAdminRequired(BadRequest, _MatchErrorMixin):
match = 'CHAT_ADMIN_REQUIRED'
text = 'Admin permissions is required!'
2018-03-30 19:32:23 +03:00
class BadWebhook(BadRequest):
pass
2018-04-22 22:02:58 +03:00
class WebhookRequireHTTPS(BadWebhook, _MatchErrorMixin):
2018-03-30 19:32:23 +03:00
match = 'HTTPS url must be provided for webhook'
text = 'bad webhook: ' + match
2018-04-22 22:02:58 +03:00
class BadWebhookPort(BadWebhook, _MatchErrorMixin):
2018-03-30 19:32:23 +03:00
match = 'Webhook can be set up only on ports 80, 88, 443 or 8443'
text = 'bad webhook: ' + match
class CantParseUrl(BadRequest, _MatchErrorMixin):
match = 'can\'t parse URL'
class NotFound(TelegramAPIError):
pass
class MethodNotKnown(NotFound, _MatchErrorMixin):
match = 'method not found'
2017-08-22 20:28:22 +03:00
class ConflictError(TelegramAPIError):
pass
2018-03-30 19:32:23 +03:00
class TerminatedByOtherGetUpdates(ConflictError, _MatchErrorMixin):
match = 'terminated by other getUpdates request'
text = 'Terminated by other getUpdates request; ' \
'Make sure that only one bot instance is running'
class CantGetUpdates(ConflictError, _MatchErrorMixin):
match = 'can\'t use getUpdates method while webhook is active'
2017-08-11 05:58:27 +03:00
class Unauthorized(TelegramAPIError):
pass
2018-03-30 19:32:23 +03:00
class BotKicked(Unauthorized, _MatchErrorMixin):
match = 'Bot was kicked from a chat'
class BotBlocked(Unauthorized, _MatchErrorMixin):
match = 'bot was blocked by the user'
class UserDeactivated(Unauthorized, _MatchErrorMixin):
match = 'user is deactivated'
2018-04-14 02:40:16 +03:00
class CantInitiateConversation(Unauthorized, _MatchErrorMixin):
match = 'bot can\'t initiate conversation with a user'
2017-08-11 05:58:27 +03:00
class NetworkError(TelegramAPIError):
pass
class RetryAfter(TelegramAPIError):
def __init__(self, retry_after):
2017-08-14 22:16:41 +03:00
super(RetryAfter, self).__init__(f"Flood control exceeded. Retry in {retry_after} seconds.")
2017-08-11 05:58:27 +03:00
self.timeout = retry_after
class MigrateToChat(TelegramAPIError):
def __init__(self, chat_id):
2017-08-14 22:16:41 +03:00
super(MigrateToChat, self).__init__(f"The group has been migrated to a supergroup. New id: {chat_id}.")
2017-08-11 05:58:27 +03:00
self.migrate_to_chat_id = chat_id
2017-12-02 01:03:56 +02:00
2018-03-30 19:32:23 +03:00
class Throttled(TelegramAPIError):
2017-12-02 01:03:56 +02:00
def __init__(self, **kwargs):
from ..dispatcher.storage import DELTA, EXCEEDED_COUNT, KEY, LAST_CALL, RATE_LIMIT, RESULT
self.key = kwargs.pop(KEY, '<None>')
self.called_at = kwargs.pop(LAST_CALL, time.time())
self.rate = kwargs.pop(RATE_LIMIT, None)
self.result = kwargs.pop(RESULT, False)
self.exceeded_count = kwargs.pop(EXCEEDED_COUNT, 0)
self.delta = kwargs.pop(DELTA, 0)
self.user = kwargs.pop('user', None)
self.chat = kwargs.pop('chat', None)
def __str__(self):
return f"Rate limit exceeded! (Limit: {self.rate} s, " \
f"exceeded: {self.exceeded_count}, " \
f"time delta: {round(self.delta, 3)} s)"