From 41b07b01addb4b9c79c33d757e9e42564637b04e Mon Sep 17 00:00:00 2001 From: Schneems Date: Tue, 25 Jun 2024 16:17:50 -0500 Subject: [PATCH] Implement std::error::Error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I want to use this in a `Box` 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. --- CHANGELOG.md | 4 ++++ src/lib.rs | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e5eb0c6..e267eff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/src/lib.rs b/src/lib.rs index abb6eaf..14c863e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 ///