diff --git a/crates/common_utils/src/errors.rs b/crates/common_utils/src/errors.rs index 9a1e681211..e3f00799e0 100644 --- a/crates/common_utils/src/errors.rs +++ b/crates/common_utils/src/errors.rs @@ -98,9 +98,26 @@ where } /// Allow [error_stack::Report] to convert between error types -/// This autoimplements [ReportSwitchExt] for the corresponding errors +/// This auto-implements [ReportSwitchExt] for the corresponding errors pub trait ErrorSwitch { /// Get the next error type that the source error can be escalated into /// This does not consume the source error since we need to keep it in context fn switch(&self) -> T; } + +/// Allow [error_stack::Report] to convert between error types +/// This serves as an alternative to [ErrorSwitch] +pub trait ErrorSwitchFrom { + /// Convert to an error type that the source can be escalated into + /// This does not consume the source error since we need to keep it in context + fn switch_from(error: &T) -> Self; +} + +impl ErrorSwitch for S +where + T: ErrorSwitchFrom, +{ + fn switch(&self) -> T { + T::switch_from(self) + } +}