From b6f97ee924979065112902422dcc785ba64e716e Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 12 Apr 2021 09:14:50 +0200 Subject: [PATCH 1/3] Add support for signed error codes --- .../source/500_INTERNAL_SERVER_ERROR.tsv | 3 ++- .../errors/source/503_SERVICE_UNAVAILABLE.tsv | 2 ++ pyrogram/errors/rpc_error.py | 24 +++++++++++++++---- 3 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 compiler/errors/source/503_SERVICE_UNAVAILABLE.tsv diff --git a/compiler/errors/source/500_INTERNAL_SERVER_ERROR.tsv b/compiler/errors/source/500_INTERNAL_SERVER_ERROR.tsv index b959bbe4..97d0984c 100644 --- a/compiler/errors/source/500_INTERNAL_SERVER_ERROR.tsv +++ b/compiler/errors/source/500_INTERNAL_SERVER_ERROR.tsv @@ -40,4 +40,5 @@ UPLOAD_NO_VOLUME Telegram is having internal problems. Please try again later VOLUME_LOC_NOT_FOUND Telegram is having internal problems. Please try again later WORKER_BUSY_TOO_LONG_RETRY Server workers are too busy right now due to Telegram having internal problems. Please try again later WP_ID_GENERATE_FAILED Telegram is having internal problems. Please try again later -GROUPCALL_ADD_PARTICIPANTS_FAILED Failure while adding voice chat member due to Telegram having internal problems. Please try again later \ No newline at end of file +GROUPCALL_ADD_PARTICIPANTS_FAILED Failure while adding voice chat member due to Telegram having internal problems. Please try again later +No workers running The Telegram server is restarting its workers. Try again later. \ No newline at end of file diff --git a/compiler/errors/source/503_SERVICE_UNAVAILABLE.tsv b/compiler/errors/source/503_SERVICE_UNAVAILABLE.tsv new file mode 100644 index 00000000..72b79cb5 --- /dev/null +++ b/compiler/errors/source/503_SERVICE_UNAVAILABLE.tsv @@ -0,0 +1,2 @@ +id message +Timeout Telegram is having internal problems. Please try again later. \ No newline at end of file diff --git a/pyrogram/errors/rpc_error.py b/pyrogram/errors/rpc_error.py index d3cb5c55..71a7249f 100644 --- a/pyrogram/errors/rpc_error.py +++ b/pyrogram/errors/rpc_error.py @@ -32,8 +32,15 @@ class RPCError(Exception): NAME = None MESSAGE = "{x}" - def __init__(self, x: Union[int, raw.types.RpcError] = None, rpc_name: str = None, is_unknown: bool = False): - super().__init__("[{} {}]: {} {}".format( + def __init__( + self, + x: Union[int, str, raw.types.RpcError] = None, + rpc_name: str = None, + is_unknown: bool = False, + is_signed: bool = False + ): + super().__init__("[{}{} {}]: {} {}".format( + "-" if is_signed else "", self.CODE, self.ID or self.NAME, self.MESSAGE.format(x=x), @@ -52,14 +59,19 @@ class RPCError(Exception): @staticmethod def raise_it(rpc_error: "raw.types.RpcError", rpc_type: Type[TLObject]): error_code = rpc_error.error_code + is_signed = error_code < 0 error_message = rpc_error.error_message rpc_name = ".".join(rpc_type.QUALNAME.split(".")[1:]) + if is_signed: + error_code = -error_code + if error_code not in exceptions: raise UnknownError( x=f"[{error_code} {error_message}]", rpc_name=rpc_name, - is_unknown=True + is_unknown=True, + is_signed=is_signed ) error_id = re.sub(r"_\d+", "_X", error_message) @@ -70,7 +82,8 @@ class RPCError(Exception): exceptions[error_code]["_"] )(x=f"[{error_code} {error_message}]", rpc_name=rpc_name, - is_unknown=True) + is_unknown=True, + is_signed=is_signed) x = re.search(r"_(\d+)", error_message) x = x.group(1) if x is not None else x @@ -80,7 +93,8 @@ class RPCError(Exception): exceptions[error_code][error_id] )(x=x, rpc_name=rpc_name, - is_unknown=False) + is_unknown=False, + is_signed=is_signed) class UnknownError(RPCError): From 70ae12eb77c3a89566e9b480635e0bbd523b4d56 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 12 Apr 2021 09:21:20 +0200 Subject: [PATCH 2/3] Handle ServiceUnavailable errors #664 --- pyrogram/session/session.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyrogram/session/session.py b/pyrogram/session/session.py index 686531e3..cf92bcf3 100644 --- a/pyrogram/session/session.py +++ b/pyrogram/session/session.py @@ -29,7 +29,7 @@ from pyrogram import __copyright__, __license__, __version__ from pyrogram import raw from pyrogram.connection import Connection from pyrogram.crypto import mtproto -from pyrogram.errors import RPCError, InternalServerError, AuthKeyDuplicated, FloodWait +from pyrogram.errors import RPCError, InternalServerError, AuthKeyDuplicated, FloodWait, ServiceUnavailable from pyrogram.raw.all import layer from pyrogram.raw.core import TLObject, MsgContainer, Int, FutureSalt, FutureSalts from .internals import MsgId, MsgFactory @@ -432,7 +432,7 @@ class Session: log.warning(f'[{self.client.session_name}] Sleeping for {amount}s (required by "{query}")') await asyncio.sleep(amount) - except (OSError, TimeoutError, InternalServerError) as e: + except (OSError, TimeoutError, InternalServerError, ServiceUnavailable) as e: if retries == 0: raise e from None From 9a8bf9d1dc625f09fd6877022bef200cc5ae97c8 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 12 Apr 2021 09:28:58 +0200 Subject: [PATCH 3/3] Fix wrongly generated class names --- compiler/errors/compiler.py | 1 + 1 file changed, 1 insertion(+) diff --git a/compiler/errors/compiler.py b/compiler/errors/compiler.py index d5cccaf4..feda9231 100644 --- a/compiler/errors/compiler.py +++ b/compiler/errors/compiler.py @@ -96,6 +96,7 @@ def start(): sub_class = caml(re.sub(r"_X", "_", error_id)) sub_class = re.sub(r"^2", "Two", sub_class) + sub_class = re.sub(r" ", "", sub_class) f_all.write(" \"{}\": \"{}\",\n".format(error_id, sub_class))