MTPyroger/docs/source/start/errors.rst

59 lines
1.7 KiB
ReStructuredText
Raw Normal View History

2018-01-06 11:18:15 +00:00
Error Handling
==============
2018-12-27 21:58:02 +00:00
Errors are inevitable when working with the API, and they must be correctly handled with ``try..except`` blocks.
2018-01-06 11:18:15 +00:00
2018-12-27 21:58:02 +00:00
There are many errors that Telegram could return, but they all fall in one of these categories
2019-05-13 16:04:44 +00:00
(which are in turn children of the ``RPCError`` superclass):
2018-01-06 11:18:15 +00:00
2019-05-13 16:04:44 +00:00
- `303 - See Other <../api/errors#see-other>`_
- `400 - Bad Request <../api/errors#bad-request>`_
- `401 - Unauthorized <../api/errors#unauthorized>`_
- `403 - Forbidden <../api/errors#forbidden>`_
- `406 - Not Acceptable <../api/errors#not-acceptable>`_
- `420 - Flood <../api/errors#flood>`_
- `500 - Internal Server Error <../api/errors#internal-server-error>`_
2018-01-06 11:18:15 +00:00
As stated above, there are really many (too many) errors, and in case Pyrogram does not know anything yet about a
2019-05-13 16:04:44 +00:00
specific one, it raises a special ``520 Unknown Error`` exception and logs it
2019-05-12 17:26:55 +00:00
in the ``unknown_errors.txt`` file. Users are invited to report these unknown errors.
2018-01-06 11:18:15 +00:00
Examples
--------
.. code-block:: python
2019-03-27 13:46:02 +00:00
from pyrogram.errors import (
2018-01-06 11:18:15 +00:00
BadRequest, Flood, InternalServerError,
SeeOther, Unauthorized, UnknownError
)
try:
2018-01-21 17:04:11 +00:00
...
2018-01-06 11:18:15 +00:00
except BadRequest:
pass
except Flood:
pass
except InternalServerError:
pass
except SeeOther:
pass
except Unauthorized:
pass
except UnknownError:
pass
2018-02-22 12:07:41 +00:00
Exception objects may also contain some informative values.
2019-05-13 16:04:44 +00:00
E.g.: ``FloodWait`` holds the amount of seconds you have to wait
2018-02-22 12:07:41 +00:00
before you can try again. The value is always stored in the ``x`` field of the returned exception object:
2018-01-06 11:18:15 +00:00
.. code-block:: python
2018-02-22 12:07:41 +00:00
import time
2019-03-27 13:46:02 +00:00
from pyrogram.errors import FloodWait
2018-01-06 11:18:15 +00:00
try:
2018-01-21 17:04:11 +00:00
...
2018-01-06 11:18:15 +00:00
except FloodWait as e:
2018-02-22 12:07:41 +00:00
time.sleep(e.x)