Skip to content

Commit

Permalink
Add PYROGRAM_DONOT_LOG_UNKNOWN_ERRORS
Browse files Browse the repository at this point in the history
  • Loading branch information
SpEcHiDe committed Feb 6, 2024
1 parent abfd8a3 commit cf38f2e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
3 changes: 3 additions & 0 deletions docs/source/api/errors/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ All Pyrogram API errors live inside the ``errors`` sub-package: ``pyrogram.error
The errors ids listed here are shown as *UPPER_SNAKE_CASE*, but the actual exception names to import from Pyrogram
follow the usual *PascalCase* convention.

There isn't any official list of all possible RPC errors, so the list of known errors is provided on a best-effort basis. When new methods are available, the list may be lacking since we simply don't know what errors can raise from them.
Pyrogram creates an `unknown_errors.txt` file in the root directory from the `Client` is run. If you do not want this file to be created, set the `PYROGRAM_DONOT_LOG_UNKNOWN_ERRORS` environment variable before running the Pyrogram `Client`. If you want the file to be created in a different location, set the `PYROGRAM_LOG_UNKNOWN_ERRORS_FILENAME` to a file path of your choice.

.. code-block:: python
from pyrogram.errors import FloodWait
Expand Down
14 changes: 12 additions & 2 deletions pyrogram/errors/rpc_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.

from os import environ
import re
from datetime import datetime
from importlib import import_module
Expand All @@ -26,6 +27,15 @@
from .exceptions.all import exceptions


UEFN = environ.get(
"PYROGRAM_LOG_UNKNOWN_ERRORS_FILENAME",
"unknown_errors.txt"
)
SCUK = bool(environ.get(
"PYROGRAM_DONOT_LOG_UNKNOWN_ERRORS"
))


class RPCError(Exception):
ID = None
CODE = None
Expand All @@ -52,8 +62,8 @@ def __init__(
except (ValueError, TypeError):
self.value = value

if is_unknown:
with open("unknown_errors.txt", "a", encoding="utf-8") as f:
if not SCUK and is_unknown:
with open(UEFN, "a", encoding="utf-8") as f:
f.write(f"{datetime.now()}\t{value}\t{rpc_name}\n")

@staticmethod
Expand Down

0 comments on commit cf38f2e

Please sign in to comment.