From 6e77f583f1660944903e3ac71824e8fcb5fc5fdb Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Fri, 12 Oct 2018 19:47:40 +0200 Subject: [PATCH] Detect arbitrary negative HTTP error codes --- telethon/errors/__init__.py | 2 +- telethon/errors/common.py | 21 +++++++++++++++------ telethon/network/mtprotoplainsender.py | 7 +++---- telethon/network/mtprotosender.py | 10 +++++++--- telethon/network/mtprotostate.py | 8 ++------ 5 files changed, 28 insertions(+), 20 deletions(-) diff --git a/telethon/errors/__init__.py b/telethon/errors/__init__.py index e3106631..03125dc5 100644 --- a/telethon/errors/__init__.py +++ b/telethon/errors/__init__.py @@ -8,7 +8,7 @@ from threading import Thread from .common import ( ReadCancelledError, TypeNotFoundError, InvalidChecksumError, - BrokenAuthKeyError, SecurityError, CdnFileTamperedError, MultiError + InvalidBufferError, SecurityError, CdnFileTamperedError, MultiError ) # This imports the base errors too, as they're imported there diff --git a/telethon/errors/common.py b/telethon/errors/common.py index 9af19c9f..65cd363c 100644 --- a/telethon/errors/common.py +++ b/telethon/errors/common.py @@ -1,4 +1,6 @@ """Errors not related to the Telegram API itself""" +import struct + from ..tl import TLRequest @@ -39,14 +41,21 @@ class InvalidChecksumError(Exception): self.valid_checksum = valid_checksum -class BrokenAuthKeyError(Exception): +class InvalidBufferError(BufferError): """ - Occurs when the authorization key for a data center is not valid. + Occurs when the buffer is invalid, and may contain an HTTP error code. + For instance, 404 means "forgotten/broken authorization key", while """ - def __init__(self): - super().__init__( - 'The authorization key is broken, and it must be reset.' - ) + def __init__(self, payload): + self.payload = payload + if len(payload) == 4: + self.code = -struct.unpack('