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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
// SPDX-License-Identifier: MPL-2.0
//! Error types for MQTT client operations
//!
//! This module provides a comprehensive error type system that distinguishes between
//! recoverable and unrecoverable errors, enabling intelligent error handling and retry logic.
use crate::mqtt_serde::parser::ParseError;
use std::fmt;
use std::io;
/// Comprehensive error type for MQTT client operations
#[derive(Debug, Clone, serde::Serialize)]
pub enum MqttClientError {
// ==================== Connection Errors (Recoverable) ====================
/// Connection refused by broker with reason code
ConnectionRefused {
reason_code: u8,
description: String,
},
/// Connection lost unexpectedly
ConnectionLost { reason: String },
/// Network I/O error occurred
NetworkError {
#[serde(skip)]
kind: io::ErrorKind,
message: String,
},
// ==================== Protocol Errors (May be recoverable) ====================
/// MQTT protocol violation detected
ProtocolViolation { message: String },
/// Failed to parse MQTT packet
PacketParsing {
parse_error: String,
raw_data: Vec<u8>, // First 100 bytes for debugging
},
/// Invalid or unexpected packet identifier
InvalidPacketId { packet_id: u16 },
/// Received unexpected packet type
UnexpectedPacket { expected: String, received: String },
// ==================== Session Errors (Recoverable) ====================
/// No active session available
NoActiveSession,
/// Session expired on broker
SessionExpired,
/// Packet ID space exhausted (all 65535 IDs in use)
PacketIdExhausted,
// ==================== Operation Errors (Recoverable) ====================
/// Operation timed out waiting for response
OperationTimeout { operation: String, timeout_ms: u64 },
/// Operation was cancelled before completion
OperationCancelled { operation: String },
/// PUBLISH operation failed
PublishFailed {
packet_id: Option<u16>,
reason_code: u8,
reason_string: Option<String>,
},
/// SUBSCRIBE operation failed
SubscribeFailed {
topics: Vec<String>,
reason_codes: Vec<u8>,
},
/// UNSUBSCRIBE operation failed
UnsubscribeFailed {
topics: Vec<String>,
reason_codes: Vec<u8>,
},
// ==================== State Errors (Application logic) ====================
/// Operation attempted in invalid connection state
InvalidState { expected: String, actual: String },
/// Not connected to broker
NotConnected,
/// Already connected to broker
AlreadyConnected,
// ==================== Resource Errors (May indicate system issues) ====================
/// Buffer is full, cannot accept more data
BufferFull {
buffer_type: String,
capacity: usize,
},
/// Internal channel closed unexpectedly
ChannelClosed { channel: String },
// ==================== Authentication Errors (Application logic) ====================
/// Authentication failed
AuthenticationFailed {
method: Option<String>,
reason_code: u8,
reason_string: Option<String>,
},
// ==================== Configuration Errors (Unrecoverable) ====================
/// Invalid client configuration
InvalidConfiguration { field: String, reason: String },
// ==================== Internal Errors (Unrecoverable) ====================
/// Internal client error (should not happen)
InternalError { message: String },
}
impl MqttClientError {
/// Returns true if the error is recoverable (retry/reconnect possible)
///
/// Recoverable errors are typically transient network issues, timeouts,
/// or session-related problems that can be resolved by retrying or reconnecting.
pub fn is_recoverable(&self) -> bool {
matches!(
self,
Self::ConnectionLost { .. }
| Self::NetworkError { .. }
| Self::OperationTimeout { .. }
| Self::SessionExpired
| Self::NotConnected
| Self::NoActiveSession
| Self::BufferFull { .. }
)
}
/// Returns true if the error should trigger automatic reconnection
///
/// These are errors that indicate the connection to the broker is broken
/// and can potentially be restored by reconnecting.
pub fn should_reconnect(&self) -> bool {
match self {
Self::ConnectionLost { .. } => true,
Self::NetworkError { kind, .. } => matches!(
kind,
io::ErrorKind::ConnectionReset
| io::ErrorKind::BrokenPipe
| io::ErrorKind::UnexpectedEof
| io::ErrorKind::ConnectionAborted
),
Self::NotConnected => false, // Already disconnected
_ => false,
}
}
/// Returns true if the error is fatal (client should stop)
///
/// Fatal errors indicate fundamental issues that cannot be resolved
/// by retrying or reconnecting.
pub fn is_fatal(&self) -> bool {
matches!(
self,
Self::InvalidConfiguration { .. }
| Self::ProtocolViolation { .. }
| Self::InternalError { .. }
)
}
/// Returns true if the error is related to authentication
pub fn is_auth_error(&self) -> bool {
matches!(
self,
Self::AuthenticationFailed { .. }
| Self::ConnectionRefused {
reason_code: 0x86 | 0x87 | 0x8C,
..
}
)
}
/// Get a user-friendly error message
///
/// This provides a clear, actionable message that can be displayed to users
/// or logged for troubleshooting.
pub fn user_message(&self) -> String {
match self {
Self::ConnectionRefused {
reason_code,
description,
} => {
format!(
"Connection refused by broker: {} (code: 0x{:02X})",
description, reason_code
)
}
Self::ConnectionLost { reason } => {
format!("Connection to broker lost: {}", reason)
}
Self::NetworkError { kind, message } => {
format!("Network error ({:?}): {}", kind, message)
}
Self::ProtocolViolation { message } => {
format!("MQTT protocol violation: {}", message)
}
Self::PacketParsing {
parse_error,
raw_data,
} => {
let data_preview = if raw_data.len() > 20 {
format!(
"{}... ({} bytes)",
hex::encode(&raw_data[..20]),
raw_data.len()
)
} else {
hex::encode(raw_data)
};
format!(
"Failed to parse MQTT packet: {} (data: {})",
parse_error, data_preview
)
}
Self::InvalidPacketId { packet_id } => {
format!("Invalid packet identifier: {}", packet_id)
}
Self::UnexpectedPacket { expected, received } => {
format!("Expected {} packet, received {}", expected, received)
}
Self::NoActiveSession => "No active session. Connect to broker first.".to_string(),
Self::SessionExpired => "Session expired on broker. Reconnection required.".to_string(),
Self::PacketIdExhausted => {
"All packet identifiers are in use. Wait for pending operations to complete."
.to_string()
}
Self::OperationTimeout {
operation,
timeout_ms,
} => {
format!(
"Operation '{}' timed out after {} ms",
operation, timeout_ms
)
}
Self::OperationCancelled { operation } => {
format!("Operation '{}' was cancelled", operation)
}
Self::PublishFailed {
packet_id,
reason_code,
reason_string,
} => {
let id_str = packet_id
.map(|id| format!(" (packet ID: {})", id))
.unwrap_or_default();
let reason_str = reason_string
.as_ref()
.map(|s| format!(": {}", s))
.unwrap_or_default();
format!(
"Publish failed{} - code: 0x{:02X}{}",
id_str, reason_code, reason_str
)
}
Self::SubscribeFailed {
topics,
reason_codes,
} => {
format!(
"Subscribe failed for topics {:?} with reason codes: {:?}",
topics, reason_codes
)
}
Self::UnsubscribeFailed {
topics,
reason_codes,
} => {
format!(
"Unsubscribe failed for topics {:?} with reason codes: {:?}",
topics, reason_codes
)
}
Self::InvalidState { expected, actual } => {
format!("Invalid state: expected {}, but was {}", expected, actual)
}
Self::NotConnected => "Not connected to broker. Call connect() first.".to_string(),
Self::AlreadyConnected => "Already connected to broker.".to_string(),
Self::BufferFull {
buffer_type,
capacity,
} => {
format!(
"{} buffer full (capacity: {}). Try again later.",
buffer_type, capacity
)
}
Self::ChannelClosed { channel } => {
format!("Internal channel '{}' closed unexpectedly", channel)
}
Self::AuthenticationFailed {
method,
reason_code,
reason_string,
} => {
let method_str = method
.as_ref()
.map(|m| format!(" (method: {})", m))
.unwrap_or_default();
let reason_str = reason_string
.as_ref()
.map(|s| format!(": {}", s))
.unwrap_or_default();
format!(
"Authentication failed{} - code: 0x{:02X}{}",
method_str, reason_code, reason_str
)
}
Self::InvalidConfiguration { field, reason } => {
format!("Invalid configuration for '{}': {}", field, reason)
}
Self::InternalError { message } => {
format!("Internal error: {}", message)
}
}
}
/// Convert from io::Error with context
///
/// # Arguments
/// * `error` - The IO error to convert
/// * `context` - Contextual information about where the error occurred
pub fn from_io_error(error: io::Error, context: &str) -> Self {
Self::NetworkError {
kind: error.kind(),
message: format!("{}: {}", context, error),
}
}
/// Convert from ParseError
pub fn from_parse_error(error: ParseError) -> Self {
Self::PacketParsing {
parse_error: error.to_string(),
raw_data: Vec::new(), // Will be populated by caller if needed
}
}
/// Convert from ParseError with raw data
pub fn from_parse_error_with_data(error: ParseError, raw_data: Vec<u8>) -> Self {
Self::PacketParsing {
parse_error: error.to_string(),
raw_data,
}
}
}
impl fmt::Display for MqttClientError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.user_message())
}
}
impl std::error::Error for MqttClientError {}
// Conversion from io::Error (without context)
impl From<io::Error> for MqttClientError {
fn from(error: io::Error) -> Self {
Self::NetworkError {
kind: error.kind(),
message: error.to_string(),
}
}
}
// Conversion to io::Error for backward compatibility
impl From<MqttClientError> for io::Error {
fn from(error: MqttClientError) -> Self {
match error {
// Map OperationTimeout to TimedOut
MqttClientError::OperationTimeout {
operation,
timeout_ms,
} => io::Error::new(
io::ErrorKind::TimedOut,
format!("{} operation timed out after {}ms", operation, timeout_ms),
),
// Map NetworkError back to io::Error (passthrough)
MqttClientError::NetworkError { kind, message } => io::Error::new(kind, message),
// Map ConnectionLost to ConnectionReset
MqttClientError::ConnectionLost { reason } => {
io::Error::new(io::ErrorKind::ConnectionReset, reason)
}
// Map NotConnected to NotConnected
MqttClientError::NotConnected => io::Error::new(
io::ErrorKind::NotConnected,
"Client is not connected to broker",
),
// Map ChannelClosed to BrokenPipe
MqttClientError::ChannelClosed { channel } => io::Error::new(
io::ErrorKind::BrokenPipe,
format!("Channel '{}' was closed unexpectedly", channel),
),
// Map BufferFull to WouldBlock (resource temporarily unavailable)
MqttClientError::BufferFull {
buffer_type,
capacity,
} => io::Error::new(
io::ErrorKind::WouldBlock,
format!("{} buffer is full (capacity: {})", buffer_type, capacity),
),
// Map InvalidConfiguration to InvalidInput
MqttClientError::InvalidConfiguration { field, reason } => io::Error::new(
io::ErrorKind::InvalidInput,
format!("Invalid configuration for '{}': {}", field, reason),
),
// Map ProtocolViolation to InvalidData
MqttClientError::ProtocolViolation { message } => {
io::Error::new(io::ErrorKind::InvalidData, message)
}
// Map PacketParsing to InvalidData
MqttClientError::PacketParsing {
parse_error,
raw_data: _,
} => io::Error::new(io::ErrorKind::InvalidData, parse_error),
// Map OperationCancelled to Interrupted
MqttClientError::OperationCancelled { operation } => io::Error::new(
io::ErrorKind::Interrupted,
format!("{} operation was cancelled", operation),
),
// Map all other errors to Other with the error's display message
other => io::Error::other(other.to_string()),
}
}
}
// Conversion from ParseError
impl From<ParseError> for MqttClientError {
fn from(error: ParseError) -> Self {
Self::from_parse_error(error)
}
}
/// Type alias for Result with MqttClientError
pub type MqttClientResult<T> = Result<T, MqttClientError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_is_recoverable() {
let recoverable_errors = vec![
MqttClientError::ConnectionLost {
reason: "test".to_string(),
},
MqttClientError::NetworkError {
kind: io::ErrorKind::ConnectionReset,
message: "test".to_string(),
},
MqttClientError::OperationTimeout {
operation: "test".to_string(),
timeout_ms: 1000,
},
MqttClientError::SessionExpired,
MqttClientError::NotConnected,
MqttClientError::NoActiveSession,
];
for error in recoverable_errors {
assert!(
error.is_recoverable(),
"Expected {:?} to be recoverable",
error
);
}
}
#[test]
fn test_is_not_recoverable() {
let unrecoverable_errors = vec![
MqttClientError::InvalidConfiguration {
field: "test".to_string(),
reason: "test".to_string(),
},
MqttClientError::ProtocolViolation {
message: "test".to_string(),
},
MqttClientError::InternalError {
message: "test".to_string(),
},
];
for error in unrecoverable_errors {
assert!(
!error.is_recoverable(),
"Expected {:?} to not be recoverable",
error
);
}
}
#[test]
fn test_should_reconnect() {
let reconnect_errors = vec![
MqttClientError::ConnectionLost {
reason: "test".to_string(),
},
MqttClientError::NetworkError {
kind: io::ErrorKind::ConnectionReset,
message: "test".to_string(),
},
MqttClientError::NetworkError {
kind: io::ErrorKind::BrokenPipe,
message: "test".to_string(),
},
];
for error in reconnect_errors {
assert!(
error.should_reconnect(),
"Expected {:?} to trigger reconnect",
error
);
}
}
#[test]
fn test_should_not_reconnect() {
let no_reconnect_errors = vec![
MqttClientError::OperationTimeout {
operation: "test".to_string(),
timeout_ms: 1000,
},
MqttClientError::NotConnected,
MqttClientError::InvalidConfiguration {
field: "test".to_string(),
reason: "test".to_string(),
},
];
for error in no_reconnect_errors {
assert!(
!error.should_reconnect(),
"Expected {:?} to not trigger reconnect",
error
);
}
}
#[test]
fn test_is_fatal() {
let fatal_errors = vec![
MqttClientError::InvalidConfiguration {
field: "test".to_string(),
reason: "test".to_string(),
},
MqttClientError::ProtocolViolation {
message: "test".to_string(),
},
MqttClientError::InternalError {
message: "test".to_string(),
},
];
for error in fatal_errors {
assert!(error.is_fatal(), "Expected {:?} to be fatal", error);
}
}
#[test]
fn test_is_auth_error() {
let auth_errors = vec![
MqttClientError::AuthenticationFailed {
method: Some("SCRAM".to_string()),
reason_code: 0x86,
reason_string: None,
},
MqttClientError::ConnectionRefused {
reason_code: 0x86, // Bad authentication method
description: "test".to_string(),
},
MqttClientError::ConnectionRefused {
reason_code: 0x87, // Not authorized
description: "test".to_string(),
},
];
for error in auth_errors {
assert!(
error.is_auth_error(),
"Expected {:?} to be auth error",
error
);
}
}
#[test]
fn test_user_message() {
let error = MqttClientError::OperationTimeout {
operation: "connect".to_string(),
timeout_ms: 5000,
};
assert_eq!(
error.user_message(),
"Operation 'connect' timed out after 5000 ms"
);
let error = MqttClientError::PublishFailed {
packet_id: Some(42),
reason_code: 0x80,
reason_string: Some("Quota exceeded".to_string()),
};
assert_eq!(
error.user_message(),
"Publish failed (packet ID: 42) - code: 0x80: Quota exceeded"
);
}
#[test]
fn test_from_io_error() {
let io_err = io::Error::new(io::ErrorKind::ConnectionReset, "connection reset");
let mqtt_err = MqttClientError::from_io_error(io_err, "write_to_transport");
match mqtt_err {
MqttClientError::NetworkError { kind, message } => {
assert_eq!(kind, io::ErrorKind::ConnectionReset);
assert!(message.contains("write_to_transport"));
}
_ => panic!("Expected NetworkError"),
}
}
#[test]
fn test_display() {
let error = MqttClientError::NotConnected;
assert_eq!(
format!("{}", error),
"Not connected to broker. Call connect() first."
);
}
}