|
17 | 17 | # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. |
18 | 18 |
|
19 | 19 | import re |
| 20 | +from datetime import datetime |
20 | 21 | from importlib import import_module |
| 22 | +from typing import Type |
21 | 23 |
|
| 24 | +from pyrogram.api.core import TLObject |
22 | 25 | from pyrogram.api.types import RpcError as RawRPCError |
23 | 26 | from .exceptions.all import exceptions |
24 | 27 |
|
25 | 28 |
|
26 | 29 | class RPCError(Exception): |
27 | | - """This is the base exception class for all Telegram API related errors. |
28 | | - For a finer grained control, see the specific errors below. |
29 | | - """ |
30 | 30 | ID = None |
31 | 31 | CODE = None |
32 | 32 | NAME = None |
33 | | - MESSAGE = None |
| 33 | + MESSAGE = "{x}" |
34 | 34 |
|
35 | | - def __init__(self, x: int or RawRPCError = None, query_type: type = None): |
36 | | - super().__init__("[{} {}]: {}".format( |
| 35 | + def __init__(self, x: int or RawRPCError, rpc_name: str, is_unknown: bool): |
| 36 | + super().__init__("[{} {}]: {} ({})".format( |
37 | 37 | self.CODE, |
38 | 38 | self.ID or self.NAME, |
39 | | - str(self) or self.MESSAGE.format(x=x) |
| 39 | + self.MESSAGE.format(x=x), |
| 40 | + 'caused by "{}"'.format(rpc_name) |
40 | 41 | )) |
41 | 42 |
|
42 | | - try: |
43 | | - self.x = int(x) |
44 | | - except (ValueError, TypeError): |
45 | | - self.x = x |
46 | | - |
47 | | - # TODO: Proper log unknown errors |
48 | | - if self.CODE == 520: |
| 43 | + if is_unknown: |
49 | 44 | with open("unknown_errors.txt", "a", encoding="utf-8") as f: |
50 | | - f.write("{}\t{}\t{}\n".format(x.error_code, x.error_message, query_type)) |
| 45 | + f.write("{}\t{}\t{}\n".format(datetime.now(), x, rpc_name)) |
51 | 46 |
|
52 | 47 | @staticmethod |
53 | | - def raise_it(rpc_error: RawRPCError, query_type: type): |
54 | | - code = rpc_error.error_code |
| 48 | + def raise_it(rpc_error: RawRPCError, rpc_type: Type[TLObject]): |
| 49 | + error_code = rpc_error.error_code |
| 50 | + error_message = rpc_error.error_message |
| 51 | + rpc_name = ".".join(rpc_type.QUALNAME.split(".")[1:]) |
55 | 52 |
|
56 | | - if code not in exceptions: |
57 | | - raise UnknownError(x=rpc_error, query_type=query_type) |
| 53 | + if error_code not in exceptions: |
| 54 | + raise UnknownError( |
| 55 | + x="[{} {}]".format(error_code, error_message), |
| 56 | + rpc_name=rpc_name, |
| 57 | + is_unknown=True |
| 58 | + ) |
58 | 59 |
|
59 | | - message = rpc_error.error_message |
60 | | - id = re.sub(r"_\d+", "_X", message) |
| 60 | + error_id = re.sub(r"_\d+", "_X", error_message) |
61 | 61 |
|
62 | | - if id not in exceptions[code]: |
63 | | - raise UnknownError(x=rpc_error, query_type=query_type) |
| 62 | + if error_id not in exceptions[error_code]: |
| 63 | + raise getattr( |
| 64 | + import_module("pyrogram.errors"), |
| 65 | + exceptions[error_code]["_"] |
| 66 | + )(x="[{} {}]".format(error_code, error_message), |
| 67 | + rpc_name=rpc_name, |
| 68 | + is_unknown=True) |
64 | 69 |
|
65 | | - x = re.search(r"_(\d+)", message) |
| 70 | + x = re.search(r"_(\d+)", error_message) |
66 | 71 | x = x.group(1) if x is not None else x |
67 | 72 |
|
68 | 73 | raise getattr( |
69 | 74 | import_module("pyrogram.errors"), |
70 | | - exceptions[code][id] |
71 | | - )(x=x) |
| 75 | + exceptions[error_code][error_id] |
| 76 | + )(x=x, |
| 77 | + rpc_name=rpc_name, |
| 78 | + is_unknown=False) |
72 | 79 |
|
73 | 80 |
|
74 | 81 | class UnknownError(RPCError): |
75 | | - """This object represents an Unknown Error, that is, an error which |
76 | | - Pyrogram does not know anything about, yet. |
77 | | - """ |
78 | 82 | CODE = 520 |
79 | 83 | """:obj:`int`: Error code""" |
80 | 84 | NAME = "Unknown error" |
81 | | - MESSAGE = "{x}" |
|
0 commit comments