Coverage for src/ramses_tx/exceptions.py: 69%

1#!/usr/bin/env python3

2"""RAMSES RF - exceptions within the packet/protocol/transport layer."""

3

4 from __future__ import annotations

5

6

7 class _RamsesBaseException(Exception):

8 """Base class for all ramses_tx exceptions."""

9

10 pass

11

12

13 class RamsesException(_RamsesBaseException):

14 """Base class for all ramses_tx exceptions."""

15

16 HINT: None | str = None

17

18 def __init__(self, *args: object):

19 super().__init__(*args)

20 self.message: str | None = args[0] if args else None # type: ignore[assignment]

21

22 def __str__(self) -> str:

23 if self.message and self.HINT:

24 return f"{self.message} (hint: {self.HINT})"

25 if self.message:

26 return self.message

27 if self.HINT:

28 return f"Hint: {self.HINT}"

29 return ""

30

31

32 class _RamsesLowerError(RamsesException):

33 """A failure in the lower layer (parser, protocol, transport, serial)."""

34

35

36########################################################################################

37# Errors at/below the protocol/transport layer, incl. packet processing

38

39

40 class ProtocolError(_RamsesLowerError):

41 """An error occurred when sending, receiving or exchanging packets."""

42

43

44 class ProtocolFsmError(ProtocolError):

45 """The protocol FSM was/became inconsistent (this shouldn't happen)."""

46

47

48 class ProtocolSendFailed(ProtocolFsmError):

49 """The Command failed to elicit an echo or (if any) the expected response."""

50

51

52 class TransportError(ProtocolError): # derived from ProtocolBaseError

53 """An error when sending or receiving frames (bytes)."""

54

55

56 class TransportSerialError(TransportError):

57 """The transport's serial port has thrown an error."""

58

59

60 class TransportSourceInvalid(TransportError):

61 """The source of packets (frames) is not valid type/configuration."""

62

63

64########################################################################################

65# Errors at/below the protocol/transport layer, incl. packet processing

66

67

68 class ParserBaseError(_RamsesLowerError):

69 """The packet is corrupt/not internally consistent, or cannot be parsed."""

70

71

72 class PacketInvalid(ParserBaseError):

73 """The packet is corrupt/not internally consistent."""

74

75

76 class PacketAddrSetInvalid(PacketInvalid):

77 """The packet's address set is inconsistent."""

78

79

80 class PacketPayloadInvalid(PacketInvalid):

81 """The packet's payload is inconsistent."""

82

83

84# Errors at/below the protocol/transport layer, incl. packet processing

85

86

87 class ParserError(ParserBaseError):

88 """The packet cannot be parsed without error."""

89

90

91 class CommandInvalid(ParserError):

92 """The command is corrupt/not internally consistent."""