Skip to content

Commit 5c0c6f4

Browse files
authored
Box currently leaking errors (#888)
1 parent 8127cd9 commit 5c0c6f4

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

espflash/src/error.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ use crate::{
1919
targets::Chip,
2020
};
2121

22+
// A type alias for a dynamic error that can be used in the library.
23+
// https://d34dl0ck.me/rust-bites-designing-error-types-in-rust-libraries/index.html
24+
type CoreError = Box<dyn core::error::Error + Send + Sync>;
25+
2226
/// All possible errors returned by espflash.
2327
#[derive(Debug, Diagnostic, Error)]
2428
#[non_exhaustive]
@@ -195,7 +199,7 @@ pub enum Error {
195199
code(espflash::invalid_elf),
196200
help("Try running `cargo clean` and rebuilding the image")
197201
)]
198-
InvalidElf(#[from] object::Error),
202+
InvalidElf(CoreError),
199203

200204
#[error("Supplied ELF image contains an invalid application descriptor")]
201205
#[diagnostic(code(espflash::invalid_app_descriptor))]
@@ -218,10 +222,10 @@ pub enum Error {
218222
#[cfg(feature = "cli")]
219223
#[error(transparent)]
220224
#[diagnostic(code(espflash::dialoguer_error))]
221-
DialoguerError(#[from] dialoguer::Error),
225+
DialoguerError(CoreError),
222226

223227
#[error(transparent)]
224-
TryFromSlice(#[from] TryFromSliceError),
228+
TryFromSlice(CoreError),
225229

226230
#[error("Failed to open file: {0}")]
227231
FileOpenError(String, #[source] io::Error),
@@ -283,6 +287,25 @@ impl From<SlipError> for Error {
283287
}
284288
}
285289

290+
impl From<TryFromSliceError> for Error {
291+
fn from(err: TryFromSliceError) -> Self {
292+
Self::TryFromSlice(Box::new(err))
293+
}
294+
}
295+
296+
impl From<object::Error> for Error {
297+
fn from(err: object::Error) -> Self {
298+
Self::InvalidElf(err.into())
299+
}
300+
}
301+
302+
#[cfg(feature = "cli")]
303+
impl From<dialoguer::Error> for Error {
304+
fn from(err: dialoguer::Error) -> Self {
305+
Self::DialoguerError(err.into())
306+
}
307+
}
308+
286309
/// App descriptor errors.
287310
#[derive(Debug, Diagnostic, Error)]
288311
#[non_exhaustive]

0 commit comments

Comments
 (0)