2018-01-06 11:18:15 +00:00
|
|
|
Error Handling
|
|
|
|
==============
|
|
|
|
|
2022-01-07 09:18:51 +00:00
|
|
|
Errors can 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
|
|
|
|
|
2020-04-01 18:08:46 +00:00
|
|
|
.. contents:: Contents
|
|
|
|
:backlinks: none
|
2020-08-22 06:05:05 +00:00
|
|
|
:depth: 1
|
2020-04-01 18:08:46 +00:00
|
|
|
:local:
|
|
|
|
|
|
|
|
-----
|
|
|
|
|
2019-05-16 19:28:34 +00:00
|
|
|
RPCError
|
|
|
|
--------
|
|
|
|
|
2020-08-22 06:05:05 +00:00
|
|
|
The father of all errors is named ``RPCError`` and is able to catch all Telegram API related errors.
|
|
|
|
This error is raised every time a method call against Telegram's API was unsuccessful.
|
2019-05-16 19:28:34 +00:00
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from pyrogram.errors import RPCError
|
|
|
|
|
2022-04-24 09:56:07 +00:00
|
|
|
.. warning::
|
2019-05-16 19:28:34 +00:00
|
|
|
|
2022-01-07 09:18:51 +00:00
|
|
|
Avoid catching this error everywhere, 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
|
2020-08-22 06:05:05 +00:00
|
|
|
provides categories of errors, which are named after the common HTTP errors and are subclass-ed from the ``RPCError``:
|
2019-05-16 19:28:34 +00:00
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from pyrogram.errors import BadRequest, Forbidden, ...
|
|
|
|
|
2020-08-22 06:05:05 +00:00
|
|
|
- :doc:`303 - SeeOther <../api/errors/see-other>`
|
|
|
|
- :doc:`400 - BadRequest <../api/errors/bad-request>`
|
|
|
|
- :doc:`401 - Unauthorized <../api/errors/unauthorized>`
|
|
|
|
- :doc:`403 - Forbidden <../api/errors/forbidden>`
|
|
|
|
- :doc:`406 - NotAcceptable <../api/errors/not-acceptable>`
|
|
|
|
- :doc:`420 - Flood <../api/errors/flood>`
|
|
|
|
- :doc:`500 - InternalServerError <../api/errors/internal-server-error>`
|
2018-01-06 11:18:15 +00:00
|
|
|
|
2019-05-17 23:45:01 +00:00
|
|
|
Single Errors
|
|
|
|
-------------
|
|
|
|
|
|
|
|
For a fine-grained control over every single error, Pyrogram does also expose errors that deal each with a specific
|
|
|
|
issue. For example:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from pyrogram.errors import FloodWait
|
|
|
|
|
|
|
|
These errors subclass directly from the category of errors they belong to, which in turn subclass from the father
|
2020-08-22 06:05:05 +00:00
|
|
|
``RPCError``, thus building a class of error hierarchy such as this:
|
2019-05-17 23:45:01 +00:00
|
|
|
|
|
|
|
- RPCError
|
|
|
|
- BadRequest
|
|
|
|
- ``MessageEmpty``
|
|
|
|
- ``UsernameOccupied``
|
|
|
|
- ``...``
|
|
|
|
- InternalServerError
|
|
|
|
- ``RpcCallFail``
|
|
|
|
- ``InterDcCallError``
|
|
|
|
- ``...``
|
|
|
|
- ``...``
|
|
|
|
|
|
|
|
.. _Errors: api/errors
|
|
|
|
|
2019-05-16 19:28:34 +00:00
|
|
|
Unknown Errors
|
|
|
|
--------------
|
2018-01-06 11:18:15 +00:00
|
|
|
|
2019-06-27 21:15:46 +00:00
|
|
|
In case Pyrogram does not know anything about a specific error yet, it raises a generic error from its known category,
|
|
|
|
for example, an unknown error with error code ``400``, will be raised as a ``BadRequest``. This way you can catch the
|
|
|
|
whole category of errors and be sure to also handle these unknown errors.
|
|
|
|
|
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
|
2019-06-27 21:15:46 +00:00
|
|
|
have to wait before you can try again, some other errors contain the DC number on which the request must be repeated on.
|
2022-04-24 09:56:07 +00:00
|
|
|
The value is stored in the ``value`` attribute of the exception object:
|
2018-01-06 11:18:15 +00:00
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
2021-05-05 11:58:47 +00:00
|
|
|
import asyncio
|
2019-03-27 13:46:02 +00:00
|
|
|
from pyrogram.errors import FloodWait
|
2018-01-06 11:18:15 +00:00
|
|
|
|
2021-05-05 11:58:47 +00:00
|
|
|
...
|
|
|
|
try:
|
|
|
|
... # Your code
|
|
|
|
except FloodWait as e:
|
2022-04-24 09:56:07 +00:00
|
|
|
await asyncio.sleep(e.value) # Wait N seconds before continuing
|
2021-05-05 11:58:47 +00:00
|
|
|
...
|