Skip to content

Commit 6845479

Browse files
authored
implement core::error::Error (#244)
by using thiserror
1 parent ebd8977 commit 6845479

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

atat/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ embassy-sync = "0.6"
2525
embassy-time = "0.4"
2626
embassy-futures = "0.1"
2727
heapless = { version = "^0.8", features = ["serde"] }
28+
thiserror = { version = "2", default-features = false }
2829
serde_at = { path = "../serde_at", version = "^0.24.1", optional = true }
2930
atat_derive = { path = "../atat_derive", version = "^0.24.1", optional = true }
3031
serde_bytes = { version = "0.11.14", default-features = false, optional = true }

atat/src/error/mod.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,43 @@ mod connection_error;
55
pub use cme_error::CmeError;
66
pub use cms_error::CmsError;
77
pub use connection_error::ConnectionError;
8+
use thiserror::Error;
89

910
/// Errors returned used internally within the crate
10-
#[derive(Clone, Debug, PartialEq, Eq)]
11+
#[derive(Clone, Debug, PartialEq, Eq, Error)]
1112
pub enum InternalError<'a> {
1213
/// Serial read error
14+
#[error("Serial read error")]
1315
Read,
1416
/// Serial write error
17+
#[error("Serial write error")]
1518
Write,
1619
/// Timed out while waiting for a response
20+
#[error("Timed out while waiting for a response")]
1721
Timeout,
1822
/// Invalid response from module
23+
#[error("Invalid response from module")]
1924
InvalidResponse,
2025
/// Command was aborted
26+
#[error("Command was aborted")]
2127
Aborted,
2228
/// Failed to parse received response
29+
#[error("Failed to parse received response")]
2330
Parse,
2431
/// Error response containing any error message
32+
#[error("Generic error response")]
2533
Error,
2634
/// GSM Equipment related error
35+
#[error("GSM Equipment related error")]
2736
CmeError(CmeError),
2837
/// GSM Network related error
38+
#[error("GSM Network related error")]
2939
CmsError(CmsError),
3040
/// Connection Error
41+
#[error("Connection Error")]
3142
ConnectionError(ConnectionError),
3243
/// Custom error match
44+
#[error("Custom error match: {0:?}")]
3345
Custom(&'a [u8]),
3446
}
3547

@@ -57,32 +69,44 @@ impl<'a> defmt::Format for InternalError<'a> {
5769
}
5870

5971
/// Errors returned by the crate
60-
#[derive(Clone, Debug, PartialEq, Eq)]
72+
#[derive(Clone, Debug, PartialEq, Eq, Error)]
6173
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
6274
pub enum Error {
6375
/// Serial read error
76+
#[error("Serial read error")]
6477
Read,
6578
/// Serial write error
79+
#[error("Serial write error")]
6680
Write,
6781
/// Timed out while waiting for a response
82+
#[error("Timed out while waiting for a response")]
6883
Timeout,
6984
/// Invalid response from module
85+
#[error("Invalid response from module")]
7086
InvalidResponse,
7187
/// Command was aborted
88+
#[error("Command was aborted")]
7289
Aborted,
7390
/// Failed to parse received response
91+
#[error("Failed to parse received response")]
7492
Parse,
7593
/// Generic error response without any error message
94+
#[error("Generic error response")]
7695
Error,
7796
/// GSM Equipment related error
97+
#[error("GSM Equipment related error")]
7898
CmeError(CmeError),
7999
/// GSM Network related error
100+
#[error("GSM Network related error")]
80101
CmsError(CmsError),
81102
/// Connection Error
103+
#[error("Connection Error")]
82104
ConnectionError(ConnectionError),
83105
/// Error response containing any error message
106+
#[error("Custom error response")]
84107
Custom,
85108
#[cfg(feature = "custom-error-messages")]
109+
#[error("Error response containing any error message {0:?}")]
86110
CustomMessage(heapless::Vec<u8, 64>),
87111
}
88112

0 commit comments

Comments
 (0)