-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.rs
More file actions
162 lines (127 loc) · 4.45 KB
/
error.rs
File metadata and controls
162 lines (127 loc) · 4.45 KB
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
//! Error types for the firmware.
use esp_idf_svc::sys::EspError;
use std::{fmt::Display, string::FromUtf8Error};
use thiserror::Error;
/// A wrapper for several different lower-level error types.
#[allow(clippy::doc_markdown)]
#[derive(Debug, Error)]
pub enum OsError {
/// Failed to initialize WiFi.
#[error("wifi init: {0}")]
WifiInit(EspError),
/// Failed to connect to WiFi AP.
#[error("wifi connect: {0}")]
WifiConnect(EspError),
/// Failed to set up a WiFi parameter.
#[error("wifi param: {0}")]
WifiParam(EspError),
/// Failed to set WiFi configuration.
#[error("wifi config: {0}")]
WifiConfig(EspError),
/// Failed to start the WiFi interface.
#[error("wifi start: {0}")]
WifiStart(EspError),
/// Failed to start AP scan or fetch the results.
#[error("wifi scan: {0}")]
WifiScan(EspError),
/// Failed to read WiFi interface information.
#[error("wifi info read: {0}")]
WifiInfo(EspError),
/// Timeout while waiting for an event.
#[error("event timeout: {0}")]
EventTimeout(EspError),
/// Failed to initialize event waiter ([Wait](esp_idf_svc::eventloop::Wait)).
#[error("event waiter: {0}")]
EventWaiterInit(EspError),
/// No usable AP found.
#[error("offline")]
NoInternet,
/// Error during connecting to or processing data from the PWMP server.
#[error("pwmp: {0}")]
PwmpError(#[from] pwmp_client::error::Error),
/// No environment sensor detected.
#[error("environment sensor")]
NoEnvSensor,
/// OTA module or update initialization has failed.
#[error("Failed to initialize an OTA update ({0})")]
OtaInit(EspError),
/// Failed to write an OTA update chunk to flash.
#[error("OTA chunk write failed ({0})")]
OtaWrite(EspError),
/// Failed to abort OTA update.
#[error("OTA abort failed ({0})")]
OtaAbort(EspError),
/// An I/O operation on an OTA slot failed.
#[error("Failed to operate on an OTA slot ({0})")]
OtaSlot(EspError),
/// NVS initialization has failed
#[error("Failed to initialize NVS ({0})")]
NvsInit(EspError),
/// Error while reading from NVS.
#[error("Failed to read from NVS ({0})")]
NvsRead(EspError),
/// Error while writing to NVS.
#[error("Failed to write to NVS ({0})")]
NvsWrite(EspError),
/// Failed to initialize a GPIO pin.
#[error("Failed to initialize a GPIO pin ({0})")]
GpioInit(EspError),
/// Failed to initialize ADC.
#[error("Failed to initialize ADC ({0})")]
AdcInit(EspError),
/// Failed to read from the ADC.
#[error("Failed to read ADC ({0})")]
AdcRead(EspError),
/// Error while reading from I2C.
#[error("Failed to read from I2C ({0})")]
I2cRead(EspError),
/// Error while writing to I2C.
#[error("Failed to write to I2C ({0})")]
I2cWrite(EspError),
/// Specified parameter was too long.
#[error("Argument too long")]
ArgumentTooLong,
/// Partition metadata contains an invalid version string.
#[error("Unexpected version format")]
IllegalFirmwareVersion,
/// Partition metadata is missing.
#[error("Unexpected version format")]
MissingPartitionMetadata,
/// A buffer has been filled unexpectedly
#[error("A buffer capacity has been exceeded")]
UnexpectedBufferFailiure,
/// A value was `None`, when `Some(..)` was expected.
#[error("Unexpected NULL")]
UnexpectedNull,
/// Expected a UTF-8 string.
#[error("String is not UTF-8 encoded")]
InvalidUtf8(#[from] FromUtf8Error),
/// Key not found in NVS storage.
#[error("NVS key does not exist")]
InvalidNvsKey,
}
/// Trait for non-fatal error types that can be "reported" to the console.
///
/// This trait is meant to be implemented for [`Result`](Result)s.
pub trait ReportableError {
/// Log a warning to the console if the [`Result`] variant is an [`Err`], or do nothing if it's [`Ok`].
fn report(self, desc: &str);
}
impl<T, E: Display> ReportableError for Result<T, E> {
fn report(self, desc: &str) {
if let Err(why) = self {
log::warn!("{desc}: {why}");
}
}
}
impl OsError {
/// Returns whether the error is non-fatal.
///
/// This returns `true`, for errors related to WiFi/Internet and PWNP connectivity/IO issues.
pub const fn recoverable(&self) -> bool {
matches!(
self,
Self::WifiConnect(..) | Self::NoInternet | Self::PwmpError(..)
)
}
}