Skip to content

Commit fa1b66f

Browse files
committed
Make unknown errors with known error codes inherit from base categories
1 parent 40bcd4e commit fa1b66f

File tree

2 files changed

+39
-34
lines changed

2 files changed

+39
-34
lines changed

compiler/error/compiler.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ def start():
8181

8282
sub_classes = []
8383

84+
f_all.write(" \"_\": \"{}\",\n".format(super_class))
85+
8486
for j, row in enumerate(reader):
8587
if j == 0:
8688
continue
@@ -90,13 +92,13 @@ def start():
9092
if not row: # Row is empty (blank line)
9193
continue
9294

93-
id, message = row
95+
error_id, error_message = row
9496

95-
sub_class = caml(re.sub(r"_X", "_", id))
97+
sub_class = caml(re.sub(r"_X", "_", error_id))
9698

97-
f_all.write(" \"{}\": \"{}\",\n".format(id, sub_class))
99+
f_all.write(" \"{}\": \"{}\",\n".format(error_id, sub_class))
98100

99-
sub_classes.append((sub_class, id, message))
101+
sub_classes.append((sub_class, error_id, error_message))
100102

101103
with open("{}/template/class.txt".format(HOME), "r", encoding="utf-8") as f_class_template:
102104
class_template = f_class_template.read()

pyrogram/errors/rpc_error.py

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,65 +17,68 @@
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

1919
import re
20+
from datetime import datetime
2021
from importlib import import_module
22+
from typing import Type
2123

24+
from pyrogram.api.core import TLObject
2225
from pyrogram.api.types import RpcError as RawRPCError
2326
from .exceptions.all import exceptions
2427

2528

2629
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-
"""
3030
ID = None
3131
CODE = None
3232
NAME = None
33-
MESSAGE = None
33+
MESSAGE = "{x}"
3434

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(
3737
self.CODE,
3838
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)
4041
))
4142

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:
4944
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))
5146

5247
@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:])
5552

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+
)
5859

59-
message = rpc_error.error_message
60-
id = re.sub(r"_\d+", "_X", message)
60+
error_id = re.sub(r"_\d+", "_X", error_message)
6161

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)
6469

65-
x = re.search(r"_(\d+)", message)
70+
x = re.search(r"_(\d+)", error_message)
6671
x = x.group(1) if x is not None else x
6772

6873
raise getattr(
6974
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)
7279

7380

7481
class UnknownError(RPCError):
75-
"""This object represents an Unknown Error, that is, an error which
76-
Pyrogram does not know anything about, yet.
77-
"""
7882
CODE = 520
7983
""":obj:`int`: Error code"""
8084
NAME = "Unknown error"
81-
MESSAGE = "{x}"

0 commit comments

Comments
 (0)