azure_iot_operations_mqtt/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Common error types

use std::fmt;

use thiserror::Error;

/// Error type for MQTT connection
pub type ConnectionError = rumqttc::v5::ConnectionError;
/// Error type for completion tokens
pub type CompletionError = rumqttc::NoticeError;
/// Error subtype for MQTT connection error caused by state
pub type StateError = rumqttc::v5::StateError;

// NOTE: While these errors may seem redundant and candidates for consolidation, we need this
// flexibility because the same error types are used in both the low-level and high-level APIs.
// If the Client/ManagedClient/PubReceiver traits were concretized, we could simplify this.

/// Error executing an MQTT publish
#[derive(Debug, Error, Clone)]
#[error("{kind}")]
pub struct PublishError {
    kind: PublishErrorKind,
}

impl PublishError {
    /// Create a new [`PublishError`]
    #[must_use]
    pub fn new(kind: PublishErrorKind) -> Self {
        Self { kind }
    }

    /// Return the corresponding [`PublishErrorKind`] for this error
    #[must_use]
    pub fn kind(&self) -> &PublishErrorKind {
        &self.kind
    }
}

/// An enumeration of categories of [`PublishError`]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PublishErrorKind {
    /// Client is detached from connection/event loop. Cannot send requests.
    DetachedClient,
    /// Invalid topic name provided
    InvalidTopicName,
}

impl fmt::Display for PublishErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            PublishErrorKind::DetachedClient => {
                write!(f, "client is detached from connection/event loop")
            }
            PublishErrorKind::InvalidTopicName => write!(f, "invalid topic name"),
        }
    }
}

/// Error executing an MQTT subscribe
#[derive(Debug, Error, Clone)]
#[error("{kind}")]
pub struct SubscribeError {
    kind: SubscribeErrorKind,
}

impl SubscribeError {
    /// Create a new [`SubscribeError`]
    #[must_use]
    pub fn new(kind: SubscribeErrorKind) -> Self {
        Self { kind }
    }

    /// Return the corresponding [`SubscribeErrorKind`] for this error
    #[must_use]
    pub fn kind(&self) -> &SubscribeErrorKind {
        &self.kind
    }
}

/// An enumeration of categories of [`SubscribeError`]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SubscribeErrorKind {
    /// Client is detached from connection/event loop. Cannot send requests.
    DetachedClient,
    /// Invalid topic filter provided
    InvalidTopicFilter,
}

impl fmt::Display for SubscribeErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            SubscribeErrorKind::DetachedClient => {
                write!(f, "client is detached from connection/event loop")
            }
            SubscribeErrorKind::InvalidTopicFilter => write!(f, "invalid topic filter"),
        }
    }
}

/// Error executing an MQTT unsubscribe
#[derive(Debug, Error, Clone)]
#[error("{kind}")]
pub struct UnsubscribeError {
    kind: UnsubscribeErrorKind,
}

impl UnsubscribeError {
    /// Create a new [`UnsubscribeError`]
    #[must_use]
    pub fn new(kind: UnsubscribeErrorKind) -> Self {
        Self { kind }
    }

    /// Return the corresponding [`UnsubscribeErrorKind`] for this error
    #[must_use]
    pub fn kind(&self) -> &UnsubscribeErrorKind {
        &self.kind
    }
}

/// An enumeration of categories of [`UnsubscribeError`]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum UnsubscribeErrorKind {
    /// Client is detached from connection/event loop. Cannot send requests.
    DetachedClient,
    /// Invalid topic filter provided
    InvalidTopicFilter,
}

impl fmt::Display for UnsubscribeErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            UnsubscribeErrorKind::DetachedClient => {
                write!(f, "client is detached from connection/event loop")
            }
            UnsubscribeErrorKind::InvalidTopicFilter => write!(f, "invalid topic filter"),
        }
    }
}

/// Error executing an MQTT ack
#[derive(Debug, Error, Clone)]
#[error("{kind}")]
pub struct AckError {
    kind: AckErrorKind,
}

impl AckError {
    /// Create a new [`AckError`]
    #[must_use]
    pub fn new(kind: AckErrorKind) -> Self {
        Self { kind }
    }

    /// Return the corresponding [`AckErrorKind`] for this error
    #[must_use]
    pub fn kind(&self) -> &AckErrorKind {
        &self.kind
    }
}

/// An enumeration of categories of [`AckError`]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum AckErrorKind {
    /// Client is detached from connection/event loop. Cannot send requests.
    DetachedClient,
    /// The publish has already been sufficiently acknowledged
    AlreadyAcked,
}

impl fmt::Display for AckErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            AckErrorKind::DetachedClient => {
                write!(f, "client is detached from connection/event loop")
            }
            AckErrorKind::AlreadyAcked => write!(f, "publish already acknowledged"),
        }
    }
}

/// Error executing an MQTT disconnect
#[derive(Debug, Error, Clone)]
#[error("{kind}")]
pub struct DisconnectError {
    kind: DisconnectErrorKind,
}

impl DisconnectError {
    /// Create a new [`DisconnectError`]
    #[must_use]
    pub fn new(kind: DisconnectErrorKind) -> Self {
        Self { kind }
    }

    /// Return the corresponding [`DisconnectErrorKind`] for this error
    #[must_use]
    pub fn kind(&self) -> &DisconnectErrorKind {
        &self.kind
    }
}

/// An enumeration of categories of [`DisconnectError`]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DisconnectErrorKind {
    /// Client is detached from connection/event loop. Cannot send requests.
    DetachedClient,
}

impl fmt::Display for DisconnectErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            DisconnectErrorKind::DetachedClient => {
                write!(f, "client is detached from connection/event loop")
            }
        }
    }
}

/// Error executing an MQTT reauth
#[derive(Debug, Error)]
#[error("{kind}")]
pub struct ReauthError {
    kind: ReauthErrorKind,
}

impl ReauthError {
    /// Create a new [`ReauthError`]
    #[must_use]
    pub fn new(kind: ReauthErrorKind) -> Self {
        Self { kind }
    }

    /// Return the corresponding [`ReauthErrorKind`] for this error
    #[must_use]
    pub fn kind(&self) -> &ReauthErrorKind {
        &self.kind
    }
}

/// An enumeration of categories of [`ReauthError`]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ReauthErrorKind {
    /// Client is detached from connection/event loop. Cannot send requests.
    DetachedClient,
}

impl fmt::Display for ReauthErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ReauthErrorKind::DetachedClient => {
                write!(f, "client is detached from connection/event loop")
            }
        }
    }
}