mirror of
https://github.com/TeamPGM/pyrogram.git
synced 2024-11-28 00:56:19 +00:00
Make unknown errors with known error codes inherit from base categories
This commit is contained in:
parent
40bcd4e59d
commit
fa1b66f914
@ -81,6 +81,8 @@ def start():
|
|||||||
|
|
||||||
sub_classes = []
|
sub_classes = []
|
||||||
|
|
||||||
|
f_all.write(" \"_\": \"{}\",\n".format(super_class))
|
||||||
|
|
||||||
for j, row in enumerate(reader):
|
for j, row in enumerate(reader):
|
||||||
if j == 0:
|
if j == 0:
|
||||||
continue
|
continue
|
||||||
@ -90,13 +92,13 @@ def start():
|
|||||||
if not row: # Row is empty (blank line)
|
if not row: # Row is empty (blank line)
|
||||||
continue
|
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:
|
with open("{}/template/class.txt".format(HOME), "r", encoding="utf-8") as f_class_template:
|
||||||
class_template = f_class_template.read()
|
class_template = f_class_template.read()
|
||||||
|
@ -17,65 +17,68 @@
|
|||||||
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
from datetime import datetime
|
||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
|
from typing import Type
|
||||||
|
|
||||||
|
from pyrogram.api.core import TLObject
|
||||||
from pyrogram.api.types import RpcError as RawRPCError
|
from pyrogram.api.types import RpcError as RawRPCError
|
||||||
from .exceptions.all import exceptions
|
from .exceptions.all import exceptions
|
||||||
|
|
||||||
|
|
||||||
class RPCError(Exception):
|
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
|
ID = None
|
||||||
CODE = None
|
CODE = None
|
||||||
NAME = None
|
NAME = None
|
||||||
MESSAGE = None
|
MESSAGE = "{x}"
|
||||||
|
|
||||||
def __init__(self, x: int or RawRPCError = None, query_type: type = None):
|
def __init__(self, x: int or RawRPCError, rpc_name: str, is_unknown: bool):
|
||||||
super().__init__("[{} {}]: {}".format(
|
super().__init__("[{} {}]: {} ({})".format(
|
||||||
self.CODE,
|
self.CODE,
|
||||||
self.ID or self.NAME,
|
self.ID or self.NAME,
|
||||||
str(self) or self.MESSAGE.format(x=x)
|
self.MESSAGE.format(x=x),
|
||||||
|
'caused by "{}"'.format(rpc_name)
|
||||||
))
|
))
|
||||||
|
|
||||||
try:
|
if is_unknown:
|
||||||
self.x = int(x)
|
|
||||||
except (ValueError, TypeError):
|
|
||||||
self.x = x
|
|
||||||
|
|
||||||
# TODO: Proper log unknown errors
|
|
||||||
if self.CODE == 520:
|
|
||||||
with open("unknown_errors.txt", "a", encoding="utf-8") as f:
|
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
|
@staticmethod
|
||||||
def raise_it(rpc_error: RawRPCError, query_type: type):
|
def raise_it(rpc_error: RawRPCError, rpc_type: Type[TLObject]):
|
||||||
code = rpc_error.error_code
|
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:
|
if error_code not in exceptions:
|
||||||
raise UnknownError(x=rpc_error, query_type=query_type)
|
raise UnknownError(
|
||||||
|
x="[{} {}]".format(error_code, error_message),
|
||||||
|
rpc_name=rpc_name,
|
||||||
|
is_unknown=True
|
||||||
|
)
|
||||||
|
|
||||||
message = rpc_error.error_message
|
error_id = re.sub(r"_\d+", "_X", error_message)
|
||||||
id = re.sub(r"_\d+", "_X", message)
|
|
||||||
|
|
||||||
if id not in exceptions[code]:
|
if error_id not in exceptions[error_code]:
|
||||||
raise UnknownError(x=rpc_error, query_type=query_type)
|
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
|
x = x.group(1) if x is not None else x
|
||||||
|
|
||||||
raise getattr(
|
raise getattr(
|
||||||
import_module("pyrogram.errors"),
|
import_module("pyrogram.errors"),
|
||||||
exceptions[code][id]
|
exceptions[error_code][error_id]
|
||||||
)(x=x)
|
)(x=x,
|
||||||
|
rpc_name=rpc_name,
|
||||||
|
is_unknown=False)
|
||||||
|
|
||||||
|
|
||||||
class UnknownError(RPCError):
|
class UnknownError(RPCError):
|
||||||
"""This object represents an Unknown Error, that is, an error which
|
|
||||||
Pyrogram does not know anything about, yet.
|
|
||||||
"""
|
|
||||||
CODE = 520
|
CODE = 520
|
||||||
""":obj:`int`: Error code"""
|
""":obj:`int`: Error code"""
|
||||||
NAME = "Unknown error"
|
NAME = "Unknown error"
|
||||||
MESSAGE = "{x}"
|
|
||||||
|
Loading…
Reference in New Issue
Block a user