Skip to content

Commit

Permalink
Implement std::error::Error
Browse files Browse the repository at this point in the history
I want to use this in a `Box<dyn Error>` but the error trait was missing an implementation.

From https://doc.rust-lang.org/std/error/trait.Error.html#method.source

> Errors may provide cause information. Error::source() is generally used when errors cross “abstraction boundaries”. If one module must report an error that is caused by an error from a lower-level module, it can allow accessing that error via Error::source(). This makes it possible for the high-level module to provide its own errors while also revealing some of the implementation for debugging.
  • Loading branch information
schneems committed Jun 25, 2024
1 parent e33d2c9 commit 41b07b0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Unreleased

## 0.2.0

- Add `std::error::Error` trait to `CmdError` ()

## 0.1.3

- Update docs on crates.io (https://github.com/schneems/fun_run/pull/7)
Expand Down
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,15 @@ impl Display for CmdError {
}
}

impl std::error::Error for CmdError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
CmdError::SystemError(_, io_err) => Some(io_err),
CmdError::NonZeroExitNotStreamed(_) | CmdError::NonZeroExitAlreadyStreamed(_) => None,
}
}
}

impl CmdError {
/// Returns a display representation of the command that failed
///
Expand Down

0 comments on commit 41b07b0

Please sign in to comment.