Errors are inevitable when working with the API, and they must be correctly handled by
the use of
There are many errors that Telegram could return, but they all fall in one of these five exception categories
(which are in turn children of the
As stated above, there are really many (too many) errors, and in case Pyrogram does not know anything yet about a
specific one, it raises a special
520 Unknown Error exception and logs it
unknown_errors.txt file. Users are invited to report these unknown errors; in later versions of Pyrogram
some kind of automatic error reporting module might be implemented.
from pyrogram.api.errors import ( BadRequest, Flood, InternalServerError, SeeOther, Unauthorized, UnknownError ) try: ... except BadRequest: pass except Flood: pass except InternalServerError: pass except SeeOther: pass except Unauthorized: pass except UnknownError: pass
Exception objects may also contain some informative values.
FloodWait holds the amount of seconds you have to wait
before you can try again. The value is always stored in the
x field of the returned exception object:
import time from pyrogram.api.errors import FloodWait try: ... except FloodWait as e: time.sleep(e.x)
TODO: Better explanation on how to deal with exceptions