pub enum Error<D> {
Domain(Box<D>),
Fault(Fault),
}
Expand description
Use Result<T, explicit_error::Error>
as the return type of any binary crate
faillible function returning errors.
The Error::Fault variant is for errors that should not happen but cannot panic.
The Error::Domain variant is for domain errors that provide feedbacks to the user.
For library or functions that require the caller to pattern match on the returned error, a dedicated type is prefered.
Variants§
Implementations§
Source§impl<D> Error<D>where
D: StdError + 'static,
impl<D> Error<D>where
D: StdError + 'static,
Sourcepub fn is_domain(&self) -> bool
pub fn is_domain(&self) -> bool
Return true if it’s a Error::Domain variant
Sourcepub fn is_fault(&self) -> bool
pub fn is_fault(&self) -> bool
Return true if it’s a Error::Fault variant
Sourcepub fn unwrap(self) -> D
pub fn unwrap(self) -> D
Unwrap the Error::Domain variant, panic otherwise
Sourcepub fn unwrap_fault(self) -> Fault
pub fn unwrap_fault(self) -> Fault
Unwrap the Error::Fault variant, panic otherwise
Sourcepub fn downcast_source_ref<E>(&self) -> Option<&E>where
E: StdError + 'static,
pub fn downcast_source_ref<E>(&self) -> Option<&E>where
E: StdError + 'static,
Try to downcast the source of the type wrapped in either Error::Domain or Error::Fault variant. If it is not set try to downcast the type wrapped. Usefull to assert_eq! in tests
§Examples
use explicit_error_exit::{ExitError, derive::ExitError, Error};
#[test]
fn test() {
assert_eq!(to_test().unwrap_err().downcast_source_ref()::<MyError>().unwrap(), &MyError::Foo);
}
#[derive(ExitError, Debug)]
enum MyError {
Foo,
}
fn to_test() -> Result<(), Error> {
Err(MyError::Foo)?;
Ok(())
}
Source§impl<D> Error<D>where
D: Domain,
impl<D> Error<D>where
D: Domain,
Sourcepub fn downcast_source<E>(
self,
) -> Result<E, Box<dyn Error + Send + Sync + 'static>>where
E: StdError + 'static,
pub fn downcast_source<E>(
self,
) -> Result<E, Box<dyn Error + Send + Sync + 'static>>where
E: StdError + 'static,
Try to downcast the source of the type wrapped in either Error::Domain or Error::Fault variant. If it is not set try to downcast the type wrapped. Usefull to assert_eq! in tests
§Examples
use explicit_error_exit::{ExitError, derive::ExitError, Error};
#[test]
fn test() {
assert_eq!(to_test().unwrap_err().downcast_source::<MyError>().unwrap(), MyError::Foo);
}
#[derive(ExitError, Debug)]
enum MyError {
Foo,
}
fn to_test() -> Result<(), Error> {
Err(MyError::Foo)?;
Ok(())
}
Sourcepub fn with_context(self, context: impl Display) -> Self
pub fn with_context(self, context: impl Display) -> Self
Add context of either Error::Domain or Error::Fault variant. Override existing context
Sourcepub fn context(&self) -> Option<&str>
pub fn context(&self) -> Option<&str>
Return the context of either Error::Domain or Error::Fault variant.