pub trait ResultFault<T, S> {
// Required methods
fn map_err_or_fault<F, E, D>(self, op: F) -> Result<T, Error<D>>
where F: FnOnce(S) -> Result<E, S>,
E: Into<Error<D>>,
S: StdError + 'static + Send + Sync,
D: Into<Error<D>>;
fn or_fault_no_source(self) -> Result<T, Fault>;
fn or_fault(self) -> Result<T, Fault>
where S: StdError + 'static + Send + Sync;
fn or_fault_no_source_force(self) -> Result<T, Fault>;
fn or_fault_force(self) -> Result<T, Fault>
where S: StdError + 'static + Send + Sync;
}
Expand description
To use this trait on Result import the prelude use explicit_error::prelude::*
Required Methods§
Sourcefn map_err_or_fault<F, E, D>(self, op: F) -> Result<T, Error<D>>
fn map_err_or_fault<F, E, D>(self, op: F) -> Result<T, Error<D>>
Convert with a closure any error wrapped in a Result to an Error. Returning an Ok convert the wrapped type to Error::Domain. Returning an Err generates a Fault with the orginal error has its source.
§Examples
Pattern match to convert to an Error::Domain
fn authz_middleware(public_identifier: &str) -> Result<(), Error> {
let entity = fetch_bar(&public_identifier).map_err_or_fault(|e|
match e {
sqlx::Error::RowNotFound => Ok(
NotFoundError::Bar(
public_identifier.to_string())),
_ => Err(e), // Convert to Error::Fault
}
)?;
Ok(entity)
}
Sourcefn or_fault_no_source(self) -> Result<T, Fault>
fn or_fault_no_source(self) -> Result<T, Fault>
Convert any Result::Err into a Result::Err wrapping a Fault Use fault instead if the error implements std::error::Error
fn foo() -> Result<(), Error> {
let file: Result<File, std::io::Error> = File::open("foo.conf");
file.or_fault_no_source().with_context("Configuration file foo.conf is missing.")?;
Err("error message").or_fault_no_source()?;
}
Sourcefn or_fault(self) -> Result<T, Fault>
fn or_fault(self) -> Result<T, Fault>
Convert any Result::Err wrapping an error that implements std::error::Error into a Result::Err wrapping a Fault
fn foo() -> Result<(), Error> {
Err(sqlx::Error::RowNotFound)
.or_fault()
.with_context("Configuration file foo.conf is missing.")?;
}
Sourcefn or_fault_no_source_force(self) -> Result<T, Fault>
fn or_fault_no_source_force(self) -> Result<T, Fault>
Convert any Result::Err into a Result::Err wrapping a Fault forcing backtrace capture Use or_fault_force instead if the error implements std::error::Error
fn foo() -> Result<(), Error> {
let file: Result<File, std::io::Error> = File::open("foo.conf");
file.or_fault_force().with_context("Configuration file foo.conf is missing.")?;
}
Sourcefn or_fault_force(self) -> Result<T, Fault>
fn or_fault_force(self) -> Result<T, Fault>
Convert any Result::Err wrapping an error that implements std::error::Error into a Result::Err wrapping a Fault forcing backtrace capture
fn foo() -> Result<(), Error> {
Err(sqlx::Error::RowNotFound)
.or_fault_force()
.with_context("Configuration file foo.conf is missing.")?;
}
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.