diff --git a/compiler/error/compiler.py b/compiler/error/compiler.py index 996c4981..80826396 100644 --- a/compiler/error/compiler.py +++ b/compiler/error/compiler.py @@ -81,6 +81,8 @@ def start(): sub_classes = [] + f_all.write(" \"_\": \"{}\",\n".format(super_class)) + for j, row in enumerate(reader): if j == 0: continue @@ -90,13 +92,13 @@ def start(): if not row: # Row is empty (blank line) continue - id, message = row + error_id, error_message = row - sub_class = caml(re.sub(r"_X", "_", id)) + sub_class = caml(re.sub(r"_X", "_", error_id)) - f_all.write(" \"{}\": \"{}\",\n".format(id, sub_class)) + f_all.write(" \"{}\": \"{}\",\n".format(error_id, sub_class)) - sub_classes.append((sub_class, id, message)) + sub_classes.append((sub_class, error_id, error_message)) with open("{}/template/class.txt".format(HOME), "r", encoding="utf-8") as f_class_template: class_template = f_class_template.read() diff --git a/pyrogram/errors/rpc_error.py b/pyrogram/errors/rpc_error.py index c1799f50..fb3a717d 100644 --- a/pyrogram/errors/rpc_error.py +++ b/pyrogram/errors/rpc_error.py @@ -17,65 +17,68 @@ # along with Pyrogram. If not, see . import re +from datetime import datetime from importlib import import_module +from typing import Type +from pyrogram.api.core import TLObject from pyrogram.api.types import RpcError as RawRPCError from .exceptions.all import exceptions class RPCError(Exception): - """This is the base exception class for all Telegram API related errors. - For a finer grained control, see the specific errors below. - """ ID = None CODE = None NAME = None - MESSAGE = None + MESSAGE = "{x}" - def __init__(self, x: int or RawRPCError = None, query_type: type = None): - super().__init__("[{} {}]: {}".format( + def __init__(self, x: int or RawRPCError, rpc_name: str, is_unknown: bool): + super().__init__("[{} {}]: {} ({})".format( self.CODE, self.ID or self.NAME, - str(self) or self.MESSAGE.format(x=x) + self.MESSAGE.format(x=x), + 'caused by "{}"'.format(rpc_name) )) - try: - self.x = int(x) - except (ValueError, TypeError): - self.x = x - - # TODO: Proper log unknown errors - if self.CODE == 520: + if is_unknown: with open("unknown_errors.txt", "a", encoding="utf-8") as f: - f.write("{}\t{}\t{}\n".format(x.error_code, x.error_message, query_type)) + f.write("{}\t{}\t{}\n".format(datetime.now(), x, rpc_name)) @staticmethod - def raise_it(rpc_error: RawRPCError, query_type: type): - code = rpc_error.error_code + def raise_it(rpc_error: RawRPCError, rpc_type: Type[TLObject]): + error_code = rpc_error.error_code + error_message = rpc_error.error_message + rpc_name = ".".join(rpc_type.QUALNAME.split(".")[1:]) - if code not in exceptions: - raise UnknownError(x=rpc_error, query_type=query_type) + if error_code not in exceptions: + raise UnknownError( + x="[{} {}]".format(error_code, error_message), + rpc_name=rpc_name, + is_unknown=True + ) - message = rpc_error.error_message - id = re.sub(r"_\d+", "_X", message) + error_id = re.sub(r"_\d+", "_X", error_message) - if id not in exceptions[code]: - raise UnknownError(x=rpc_error, query_type=query_type) + if error_id not in exceptions[error_code]: + raise getattr( + import_module("pyrogram.errors"), + exceptions[error_code]["_"] + )(x="[{} {}]".format(error_code, error_message), + rpc_name=rpc_name, + is_unknown=True) - x = re.search(r"_(\d+)", message) + x = re.search(r"_(\d+)", error_message) x = x.group(1) if x is not None else x raise getattr( import_module("pyrogram.errors"), - exceptions[code][id] - )(x=x) + exceptions[error_code][error_id] + )(x=x, + rpc_name=rpc_name, + is_unknown=False) class UnknownError(RPCError): - """This object represents an Unknown Error, that is, an error which - Pyrogram does not know anything about, yet. - """ CODE = 520 """:obj:`int`: Error code""" NAME = "Unknown error" - MESSAGE = "{x}"