From 4315ddbb57884a0a627a785a1d576dca4c3920ff Mon Sep 17 00:00:00 2001 From: Jahongir Qurbonov Date: Tue, 3 Jun 2025 09:41:06 +0500 Subject: [PATCH] Refactor error handling in deserialize method to use specific exception classes --- .../src/telethon/_impl/mtproto/mtp/plain.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/client/src/telethon/_impl/mtproto/mtp/plain.py b/client/src/telethon/_impl/mtproto/mtp/plain.py index 15e86caf..8dff2b19 100644 --- a/client/src/telethon/_impl/mtproto/mtp/plain.py +++ b/client/src/telethon/_impl/mtproto/mtp/plain.py @@ -2,7 +2,16 @@ import struct from typing import Optional from ..utils import check_message_buffer -from .types import Deserialization, MsgId, Mtp, RpcResult +from .types import ( + BadAuthKeyError, + BadMsgIdError, + Deserialization, + MsgId, + Mtp, + NegativeLengthError, + RpcResult, + TooLongMsgError, +) class Plain(Mtp): @@ -38,17 +47,17 @@ class Plain(Mtp): auth_key_id, msg_id, length = struct.unpack_from("= 0, got: {length}") + raise NegativeLengthError(got=length) if 20 + length > (lp := len(payload)): - raise ValueError(f"message too short, expected: {20 + length}, got {lp}") + raise TooLongMsgError(got=length, max_length=lp - 20) return [RpcResult(MsgId(0), bytes(payload[20 : 20 + length]))]