Coverage for zenmqtt/exceptions.py: 100%

26 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-12 21:55 +0000

1from typing import TYPE_CHECKING, Optional 

2 

3if TYPE_CHECKING: 

4 from zenmqtt.mqtt.connect import ServerDisconnect 

5 

6 

7class MQTTError(Exception): 

8 """Base of all errors of the client: except MQTTError catches any of them.""" 

9 

10 

11class ClientConnectionError(MQTTError): 

12 """The client isn't connected or the connection was lost.""" 

13 

14 

15class NotConnectedError(ClientConnectionError): 

16 """Operation was requested while the client isn't connected.""" 

17 

18 

19class ConnectionLostError(ClientConnectionError): 

20 """ 

21 Connection was lost before the operation was acknowledged. 

22 

23 QoS 1/2 messages stay in the session and are re-sent on the next connect, 

24 unless the server reports that the session isn't present. The server keeps 

25 the session only if "session_expiry_interval" > 0 is set on connect. 

26 

27 server_disconnect is the DISCONNECT packet of the server, if the server 

28 closed the connection with it. 

29 """ 

30 

31 def __init__(self, server_disconnect: Optional["ServerDisconnect"] = None): 

32 super().__init__(server_disconnect or "Connection lost") 

33 self.server_disconnect = server_disconnect 

34 

35 

36class SessionLostError(ConnectionLostError): 

37 """ 

38 The server didn't keep the session after reconnect, so the pending QoS 1/2 

39 message was discarded and won't be delivered. 

40 """ 

41 

42 def __init__(self) -> None: 

43 super().__init__() 

44 self.args = ("The server didn't keep the session, the message was discarded",) 

45 

46 

47class ServerLimitError(MQTTError, ValueError): 

48 """ 

49 The operation breaks a limit which the server sent in CONNACK; 

50 nothing is sent to the server. 

51 """ 

52 

53 

54class QoSNotSupportedError(ServerLimitError): 

55 """QoS of the message is higher than "Maximum QoS" of the server.""" 

56 

57 

58class OutgoingPacketTooLargeError(ServerLimitError): 

59 """The packet is bigger than "Maximum Packet Size" of the server.""" 

60 

61 

62class FeatureNotSupportedError(ServerLimitError): 

63 """ 

64 The server doesn't support the feature: retained messages, topic aliases, 

65 wildcard, shared subscriptions or subscription identifiers. 

66 """ 

67 

68 

69class ProtocolError(MQTTError, ValueError): 

70 """ 

71 A received packet violates the protocol; the client closes the connection 

72 with DISCONNECT and the reason code of the error. 

73 """ 

74 

75 reason_code = 0x82 

76 

77 

78class MalformedPacketError(ProtocolError): 

79 """A received packet can't be parsed according to the spec.""" 

80 

81 reason_code = 0x81 

82 

83 

84class IncomingPacketTooLargeError(ProtocolError): 

85 """The server sent a packet bigger than "Maximum Packet Size" of the client.""" 

86 

87 reason_code = 0x95 

88 

89 

90class ReceiveMaximumExceededError(ProtocolError): 

91 """The server sent more QoS 1/2 messages than the client's "Receive Maximum".""" 

92 

93 reason_code = 0x93 

94 

95 

96class TopicAliasInvalidError(ProtocolError): 

97 """The server sent a topic alias of 0 or above client's "Topic Alias Maximum".""" 

98 

99 reason_code = 0x94