From ad62c6b6183840233da0b723e16dcb2583add07b Mon Sep 17 00:00:00 2001 From: Vert Date: Sun, 12 May 2024 12:03:20 -0400 Subject: [PATCH] add new exceptions --- rcon/exceptions.py | 4 ++++ rcon/source/proto.py | 10 ++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/rcon/exceptions.py b/rcon/exceptions.py index 0ada898..134b2a5 100644 --- a/rcon/exceptions.py +++ b/rcon/exceptions.py @@ -27,3 +27,7 @@ class UserAbort(Exception): class WrongPassword(Exception): """Indicates a wrong password.""" + + +class UnexpectedTerminator(Exception): + """Indicates an unexpected terminator in the response.""" diff --git a/rcon/source/proto.py b/rcon/source/proto.py index dc0c1d2..fe7d929 100644 --- a/rcon/source/proto.py +++ b/rcon/source/proto.py @@ -8,7 +8,7 @@ from logging import getLogger from random import randint from typing import IO, NamedTuple -from rcon.exceptions import EmptyResponse +from rcon.exceptions import EmptyResponse, UnexpectedTerminator __all__ = ["LittleEndianSignedInt32", "Type", "Packet", "random_request_id"] @@ -118,7 +118,9 @@ class Packet(NamedTuple): return size + payload @classmethod - async def aread(cls, reader: StreamReader, raise_unexpected_terminator: bool = False) -> Packet: + async def aread( + cls, reader: StreamReader, raise_unexpected_terminator: bool = False + ) -> Packet: """Read a packet from an asynchronous file-like object.""" LOGGER.debug("Reading packet asynchronously.") size = await LittleEndianSignedInt32.aread(reader) @@ -138,7 +140,7 @@ class Packet(NamedTuple): if terminator != TERMINATOR: if raise_unexpected_terminator: - raise ValueError("Unexpected terminator") + raise UnexpectedTerminator(terminator) LOGGER.warning("Unexpected terminator: %s", terminator) return cls(id_, type_, payload, terminator) @@ -164,7 +166,7 @@ class Packet(NamedTuple): if terminator != TERMINATOR: if raise_unexpected_terminator: - raise ValueError("Unexpected terminator") + raise UnexpectedTerminator(terminator) LOGGER.warning("Unexpected terminator: %s", terminator) return cls(id_, type_, payload, terminator)