mqute_codec/error.rs
1//! # MQTT Error Handling
2//!
3//! This module defines the `Error` enum, which represents various errors that can occur
4//! when working with the MQTT protocol. The enum is derived using the `thiserror` crate,
5//! which provides convenient error handling and formatting.
6
7use std::io;
8
9/// Represents errors that can occur when working with the MQTT protocol.
10///
11/// Each variant includes a descriptive error message and, where applicable,
12/// additional context (e.g., invalid values or sizes). This enum implements
13/// the standard `Error` trait and can be easily converted from other error types.
14#[derive(Debug, thiserror::Error)]
15pub enum Error {
16 /// Indicates an invalid connect return code received from the broker.
17 #[error("Invalid connect return code: {0}")]
18 InvalidConnectReturnCode(u8),
19
20 /// Indicates an invalid packet type encountered during parsing.
21 #[error("Invalid packet type: {0}")]
22 InvalidPacketType(u8),
23
24 /// Indicates an unsupported or invalid protocol level.
25 #[error("Invalid protocol level: {0}")]
26 InvalidProtocolLevel(u8),
27
28 /// Indicates an invalid or unrecognized protocol name.
29 #[error("Invalid protocol name: {0}")]
30 InvalidProtocolName(String),
31
32 /// Indicates an invalid property identifier in MQTT properties.
33 #[error("Invalid property: {0}")]
34 InvalidProperty(u8),
35
36 /// Indicates an invalid Quality of Service level.
37 #[error("Invalid QoS: {0} (must be 0, 1, or 2)")]
38 InvalidQos(u8),
39
40 /// Indicates an invalid reason code in MQTT response packets.
41 #[error("Invalid reason code: {0}")]
42 InvalidReasonCode(u8),
43
44 /// Indicates invalid retain handling configuration.
45 #[error("Invalid retain handling: {0}")]
46 InvalidRetainHandling(u8),
47
48 /// Indicates a malformed or invalid topic name.
49 #[error("Invalid topic name: {0}")]
50 InvalidTopicName(String),
51
52 /// Indicates a malformed or invalid topic filter.
53 #[error("Invalid topic filter: {0}")]
54 InvalidTopicFilter(String),
55
56 /// Indicates invalid UTF-8 data in string fields.
57 #[error("Invalid UTF-8 data in string field")]
58 InvalidUtf8,
59
60 /// Wraps an I/O error that occurred during network operations.
61 #[error("IO error: {0}")]
62 Io(#[from] io::Error),
63
64 /// Indicates a malformed variable byte integer encoding.
65 #[error("Malformed variable byte integer encoding")]
66 MalformedVariableByteInteger,
67
68 /// Indicates a general malformed packet structure.
69 #[error("Malformed packet structure")]
70 MalformedPacket,
71
72 /// Indicates that the payload must contain at least one return code but was empty.
73 #[error("The payload of packet must contain at least one return code")]
74 NoCodes,
75
76 /// Indicates insufficient bytes to complete packet framing.
77 #[error("Insufficient bytes to frame packet: expected at least {0} more bytes")]
78 NotEnoughBytes(usize),
79
80 /// Indicates that the payload must contain at least one topic filter but was empty.
81 #[error("The payload of packet must contain at least one topic filter")]
82 NoTopic,
83
84 /// Indicates that the outgoing payload exceeds the configured size limit.
85 #[error("Outgoing payload size limit exceeded: {0} bytes")]
86 OutgoingPayloadSizeLimitExceeded(usize),
87
88 /// Indicates an out-of-bounds access during packet parsing.
89 #[error("Out of bounds access during packet parsing")]
90 OutOfBounds,
91
92 /// Indicates that the payload exceeds the maximum allowed size.
93 #[error("Payload too large for the current configuration")]
94 PayloadTooLarge,
95
96 /// Indicates that a payload is required for this packet type but was missing.
97 #[error("Payload required for this packet type")]
98 PayloadRequired,
99
100 /// Indicates that the payload exceeds the general size limit.
101 #[error("Payload size limit exceeded: {0} bytes")]
102 PayloadSizeLimitExceeded(usize),
103
104 /// Indicates a general protocol violation error.
105 #[error("Protocol violation detected")]
106 ProtocolError,
107
108 /// Indicates a mismatch between expected and actual protocol versions.
109 #[error("Protocol version mismatch")]
110 ProtocolMismatch,
111
112 /// Indicates that the requested protocol is not supported.
113 #[error("Protocol not supported by this implementation")]
114 ProtocolNotSupported,
115
116 /// Indicates a mismatch between expected and actual properties.
117 #[error("Property mismatch between expected and received values")]
118 PropertyMismatch,
119
120 /// Indicates that a string exceeds the maximum allowed length.
121 #[error("String exceeds maximum allowed length")]
122 StringTooLong,
123}