Skip to content

Commit

Permalink
try a different approach for source() impl for wrapper opaque Errors
Browse files Browse the repository at this point in the history
  • Loading branch information
GlenDC committed Sep 9, 2024
1 parent 68adc8c commit 9d66dbe
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
6 changes: 5 additions & 1 deletion rama-cli/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ impl std::fmt::Display for ErrorWithExitCode {

impl std::error::Error for ErrorWithExitCode {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.error.source()
if let Some(err) = self.error.source() {
return Some(err);
}
let err = self.error.as_ref();
Some(err as &(dyn std::error::Error + 'static))
}
}
6 changes: 5 additions & 1 deletion rama-error/src/ext/backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ impl<E: Error> Debug for BacktraceError<E> {

impl<E: Error + 'static> Error for BacktraceError<E> {
fn source(&self) -> Option<&(dyn Error + 'static)> {
self.inner.source()
if let Some(err) = self.inner.source() {
return Some(err);
}
let err = &self.inner;
Some(err as &(dyn std::error::Error + 'static))
}
}
6 changes: 5 additions & 1 deletion rama-error/src/ext/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ where
E: std::error::Error + 'static,
{
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.error.source()
if let Some(err) = self.error.source() {
return Some(err);
}
let err = &self.error;
Some(err as &(dyn std::error::Error + 'static))
}
}

Expand Down
6 changes: 5 additions & 1 deletion rama-error/src/ext/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,11 @@ mod tests {

impl std::error::Error for WrapperError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(self.0.as_ref())
if let Some(err) = self.0.source() {
return Some(err);
}
let err = self.0.as_ref();
Some(err as &(dyn std::error::Error + 'static))
}
}

Expand Down
6 changes: 5 additions & 1 deletion rama-error/src/ext/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ impl Display for OpaqueError {

impl std::error::Error for OpaqueError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.0.source()
if let Some(err) = self.0.source() {
return Some(err);
}
let err = self.0.as_ref();
Some(err as &(dyn std::error::Error + 'static))
}
}

Expand Down

0 comments on commit 9d66dbe

Please sign in to comment.