MTPyroger/docs/source/start/errors.rst

65 lines
2.1 KiB
ReStructuredText
Raw Normal View History

2018-01-06 11:18:15 +00:00
Error Handling
==============
2019-05-16 19:28:34 +00:00
Errors are inevitable when working with the API, and they must be correctly handled with ``try..except`` blocks in order
to control the behaviour of your application. Pyrogram errors all live inside the ``errors`` package:
2018-01-06 11:18:15 +00:00
2019-05-16 19:28:34 +00:00
.. code-block:: python
from pyrogram import errors
RPCError
--------
The father of all errors is named ``RPCError``. This error exists in form of a Python exception and is able to catch all
Telegram API related errors.
.. code-block:: python
from pyrogram.errors import RPCError
.. warning::
It must be noted that catching this error is bad practice, especially when no feedback is given (i.e. by
logging/printing the full error traceback), because it makes it impossible to understand what went wrong.
2018-01-06 11:18:15 +00:00
2019-05-16 19:28:34 +00:00
Error Categories
----------------
The ``RPCError`` packs together all the possible errors Telegram could raise, but to make things tidier, Pyrogram
provides categories of errors, which are named after the common HTTP errors:
.. code-block:: python
from pyrogram.errors import BadRequest, Forbidden, ...
- `303 - SeeOther <../api/errors#seeother>`_
- `400 - BadRequest <../api/errors#badrequest>`_
2019-05-13 16:04:44 +00:00
- `401 - Unauthorized <../api/errors#unauthorized>`_
- `403 - Forbidden <../api/errors#forbidden>`_
2019-05-16 19:28:34 +00:00
- `406 - NotAcceptable <../api/errors#notacceptable>`_
2019-05-13 16:04:44 +00:00
- `420 - Flood <../api/errors#flood>`_
2019-05-16 19:28:34 +00:00
- `500 - InternalServerError <../api/errors#internalservererror>`_
2018-01-06 11:18:15 +00:00
2019-05-16 19:28:34 +00:00
Unknown Errors
--------------
2018-01-06 11:18:15 +00:00
2019-05-16 19:28:34 +00:00
In case Pyrogram does not know anything yet about a specific error, it raises a special ``520 - UnknownError`` exception
and logs it in the ``unknown_errors.txt`` file. Users are invited to report these unknown errors.
2018-01-06 11:18:15 +00:00
2019-05-16 19:28:34 +00:00
Errors with Values
------------------
2018-01-06 11:18:15 +00:00
2019-05-16 19:28:34 +00:00
Exception objects may also contain some informative values. For example, ``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:
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:
2019-05-16 19:28:34 +00:00
time.sleep(e.x) # Wait before trying again