mqute_codec/
error.rs

1//! # 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).
13#[derive(Debug, thiserror::Error)]
14pub enum Error {
15    /// Indicates an invalid connect return code.
16    #[error("Invalid connect return code: {0}")]
17    InvalidConnectReturnCode(u8),
18
19    /// Indicates an invalid packet type.
20    #[error("Invalid packet type: {0}")]
21    InvalidPacketType(u8),
22
23    /// Indicates an invalid reason code.
24    #[error("Invalid reason code: {0}")]
25    InvalidReasonCode(u8),
26
27    /// Indicates an invalid protocol name.
28    #[error("Invalid protocol name: {0}")]
29    InvalidProtocolName(String),
30
31    /// Indicates an invalid protocol level.
32    #[error("Invalid protocol level: {0}")]
33    InvalidProtocolLevel(u8),
34
35    /// Indicates an invalid property.
36    #[error("Invalid property: {0}")]
37    InvalidProperty(u8),
38
39    /// Indicates invalid UTF-8 data.
40    #[error("Invalid UTF-8")]
41    InvalidUtf8,
42
43    /// Indicates an invalid QoS level.
44    #[error("Invalid QoS: {0}")]
45    InvalidQos(u8),
46
47    /// Indicates invalid retain handling.
48    #[error("Invalid retain handling: {0}")]
49    InvalidRetainHandling(u8),
50
51    /// Wraps an I/O error.
52    #[error("IO error: {0}")]
53    Io(#[from] io::Error),
54
55    /// Indicates a malformed variable byte integer.
56    #[error("Malformed variable byte integer")]
57    MalformedVariableByteInteger,
58
59    /// Indicates a malformed packet.
60    #[error("Malformed packet")]
61    MalformedPacket,
62
63    /// Indicates that the payload must contain at least one return code.
64    #[error("The payload of packet must contain at least one return code")]
65    NoCodes,
66
67    /// Indicates that there are not enough bytes to frame the packet.
68    #[error("At least there are not enough {0} bytes to frame the packet")]
69    NotEnoughBytes(usize),
70
71    /// Indicates that the payload must contain at least one topic filter.
72    #[error("The payload of packet must contain at least one topic filter")]
73    NoTopic,
74
75    /// Indicates that the outgoing payload size limit has been exceeded.
76    #[error("Outgoing payload size limit exceeded: {0}")]
77    OutgoingPayloadSizeLimitExceeded(usize),
78
79    /// Indicates an out-of-bounds access.
80    #[error("Out of bounds")]
81    OutOfBounds,
82
83    /// Indicates that the payload is too large.
84    #[error("Payload too large")]
85    PayloadTooLarge,
86
87    /// Indicates that a payload is required but missing.
88    #[error("Payload required")]
89    PayloadRequired,
90
91    /// Indicates that the payload size limit has been exceeded.
92    #[error("Payload size limit exceeded: {0}")]
93    PayloadSizeLimitExceeded(usize),
94
95    /// Indicates a protocol error.
96    #[error("Protocol error")]
97    ProtocolError,
98
99    /// Indicates a protocol mismatch.
100    #[error("Protocol mismatch")]
101    ProtocolMismatch,
102
103    /// Indicates that the protocol is not supported.
104    #[error("Protocol not supported")]
105    ProtocolNotSupported,
106
107    /// Indicates a property mismatch.
108    #[error("Property mismatch")]
109    PropertyMismatch,
110
111    /// Indicates that a string is too long.
112    #[error("String too long")]
113    StringTooLong,
114}