File size: 1,847 Bytes
71e71e8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | """
Drop-in replacements for the ``telegram.error`` exception hierarchy, so that
every ``except BadRequest`` / ``except Forbidden`` / ``except TelegramError``
clause in the original codebase keeps working unchanged.
These are pure Python exception classes - they carry no Telethon-specific
behaviour. All the translation from real Telethon/MTProto errors into these
types happens in ``src/tg_compat/_bot.py``.
"""
class TelegramError(Exception):
"""Base class for all compat Telegram errors (mirrors telegram.error.TelegramError)."""
def __init__(self, message: str = ""):
self.message = message
super().__init__(message)
def __str__(self):
return self.message
class BadRequest(TelegramError):
"""Mirrors telegram.error.BadRequest (Bot API 'Bad Request' responses)."""
class Forbidden(TelegramError):
"""Mirrors telegram.error.Forbidden (bot was blocked / kicked / lacks rights)."""
class TimedOut(TelegramError):
"""Mirrors telegram.error.TimedOut (request timed out)."""
def __init__(self, message: str = "Timed out"):
super().__init__(message)
class NetworkError(TelegramError):
"""Mirrors telegram.error.NetworkError (connection-level failures)."""
class RetryAfter(TelegramError):
"""Mirrors telegram.error.RetryAfter (flood control)."""
def __init__(self, retry_after):
self.retry_after = retry_after
super().__init__(f"Flood control exceeded. Retry in {retry_after} seconds")
class ChatMigrated(TelegramError):
"""Mirrors telegram.error.ChatMigrated (basic group upgraded to supergroup)."""
def __init__(self, new_chat_id: int):
self.new_chat_id = new_chat_id
super().__init__(f"Group migrated to supergroup {new_chat_id}")
class InvalidToken(TelegramError):
"""Mirrors telegram.error.InvalidToken."""
|