Browse Source

add new exceptions

pull/31/head
Vert 11 months ago
parent
commit
ad62c6b618
  1. 4
      rcon/exceptions.py
  2. 10
      rcon/source/proto.py

4
rcon/exceptions.py

@ -27,3 +27,7 @@ class UserAbort(Exception):
class WrongPassword(Exception): class WrongPassword(Exception):
"""Indicates a wrong password.""" """Indicates a wrong password."""
class UnexpectedTerminator(Exception):
"""Indicates an unexpected terminator in the response."""

10
rcon/source/proto.py

@ -8,7 +8,7 @@ from logging import getLogger
from random import randint from random import randint
from typing import IO, NamedTuple from typing import IO, NamedTuple
from rcon.exceptions import EmptyResponse from rcon.exceptions import EmptyResponse, UnexpectedTerminator
__all__ = ["LittleEndianSignedInt32", "Type", "Packet", "random_request_id"] __all__ = ["LittleEndianSignedInt32", "Type", "Packet", "random_request_id"]
@ -118,7 +118,9 @@ class Packet(NamedTuple):
return size + payload return size + payload
@classmethod @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.""" """Read a packet from an asynchronous file-like object."""
LOGGER.debug("Reading packet asynchronously.") LOGGER.debug("Reading packet asynchronously.")
size = await LittleEndianSignedInt32.aread(reader) size = await LittleEndianSignedInt32.aread(reader)
@ -138,7 +140,7 @@ class Packet(NamedTuple):
if terminator != TERMINATOR: if terminator != TERMINATOR:
if raise_unexpected_terminator: if raise_unexpected_terminator:
raise ValueError("Unexpected terminator") raise UnexpectedTerminator(terminator)
LOGGER.warning("Unexpected terminator: %s", terminator) LOGGER.warning("Unexpected terminator: %s", terminator)
return cls(id_, type_, payload, terminator) return cls(id_, type_, payload, terminator)
@ -164,7 +166,7 @@ class Packet(NamedTuple):
if terminator != TERMINATOR: if terminator != TERMINATOR:
if raise_unexpected_terminator: if raise_unexpected_terminator:
raise ValueError("Unexpected terminator") raise UnexpectedTerminator(terminator)
LOGGER.warning("Unexpected terminator: %s", terminator) LOGGER.warning("Unexpected terminator: %s", terminator)
return cls(id_, type_, payload, terminator) return cls(id_, type_, payload, terminator)

Loading…
Cancel
Save