From 56595e4a9ce1beb89b870300ee0a168d0196e730 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Tue, 23 Apr 2019 11:28:09 +0200 Subject: [PATCH] Treat all error codes as positive This means that -500 errors will now behave like 500 errors correctly so the -500 "No workers running" will properly be caught and the library will retry requests by default. --- telethon/errors/__init__.py | 8 +++++++- telethon/errors/rpcbaseerrors.py | 4 ++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/telethon/errors/__init__.py b/telethon/errors/__init__.py index ef327041..af1e089c 100644 --- a/telethon/errors/__init__.py +++ b/telethon/errors/__init__.py @@ -34,7 +34,13 @@ def rpc_message_to_error(rpc_error, request): capture = int(m.group(1)) if m.groups() else None return cls(request, capture=capture) - cls = base_errors.get(rpc_error.error_code) + # Some errors are negative: + # * -500 for "No workers running", + # * -503 for "Timeout" + # + # We treat them as if they were positive, so -500 will be treated + # as a `ServerError`, etc. + cls = base_errors.get(abs(rpc_error.error_code)) if cls: return cls(request, rpc_error.error_message) diff --git a/telethon/errors/rpcbaseerrors.py b/telethon/errors/rpcbaseerrors.py index edbab5ba..fcbbc8b3 100644 --- a/telethon/errors/rpcbaseerrors.py +++ b/telethon/errors/rpcbaseerrors.py @@ -100,7 +100,7 @@ class ServerError(RPCError): for example, there was a disruption while accessing a database or file storage. """ - code = 500 + code = 500 # Also witnessed as -500 message = 'INTERNAL' def __init__(self, request, message): @@ -113,7 +113,7 @@ class BotTimeout(RPCError): Clicking the inline buttons of bots that never (or take to long to) call ``answerCallbackQuery`` will result in this "special" RPCError. """ - code = -503 + code = 503 # Only witnessed as -503 message = 'Timeout' def __init__(self, request, message):