From cf38f2ec3ca44af1973b869dd2a074896c19073c Mon Sep 17 00:00:00 2001 From: shriMADhav U k Date: Tue, 6 Feb 2024 13:05:54 +0100 Subject: [PATCH] Add `PYROGRAM_DONOT_LOG_UNKNOWN_ERRORS` https://t.me/pyrogramchat/607757 --- docs/source/api/errors/index.rst | 3 +++ pyrogram/errors/rpc_error.py | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/docs/source/api/errors/index.rst b/docs/source/api/errors/index.rst index be2b80d46..87d89b1a6 100644 --- a/docs/source/api/errors/index.rst +++ b/docs/source/api/errors/index.rst @@ -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 diff --git a/pyrogram/errors/rpc_error.py b/pyrogram/errors/rpc_error.py index 63375f199..506103e9d 100644 --- a/pyrogram/errors/rpc_error.py +++ b/pyrogram/errors/rpc_error.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from os import environ import re from datetime import datetime from importlib import import_module @@ -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 @@ -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