Skip to main content

mqtt_proto/
error.rs

1//! Error type shared by the codec, the client and the broker.
2
3/// Convenience alias for `Result<T, MqttError>`.
4pub type MqttResult<T> = Result<T, MqttError>;
5
6/// All errors that can be produced while encoding/decoding MQTT packets or
7/// operating an MQTT client (`stem-mqtt-client`). The client crate exposes it to
8/// foreign languages through UniFFI as a plain error enum.
9#[derive(Debug, thiserror::Error)]
10pub enum MqttError {
11    /// The peer sent a packet that violates the MQTT specification.
12    #[error("malformed packet: {0}")]
13    MalformedPacket(String),
14
15    /// A value could not be encoded/decoded per the wire protocol.
16    #[error("protocol error: {0}")]
17    Protocol(String),
18
19    /// Underlying I/O failure (connection reset, DNS failure, etc.).
20    #[error("io error: {0}")]
21    Io(String),
22
23    /// The broker rejected the connection attempt.
24    #[error("connection refused: {0}")]
25    ConnectionRefused(String),
26
27    /// An operation was attempted while not connected to a broker.
28    #[error("not connected")]
29    NotConnected,
30
31    /// A packet's declared size exceeds the configured maximum packet size.
32    #[error("packet too large: {0}")]
33    PacketTooLarge(String),
34
35    /// The client is already connected.
36    #[error("already connected")]
37    AlreadyConnected,
38
39    /// The peer did not acknowledge a QoS 1/2 packet before the timeout.
40    #[error("operation timed out")]
41    Timeout,
42
43    /// Generic keep-alive / session error.
44    #[error("session error: {0}")]
45    Session(String),
46}
47
48impl From<std::io::Error> for MqttError {
49    fn from(e: std::io::Error) -> Self {
50        MqttError::Io(e.to_string())
51    }
52}