pub struct DomainError {
pub output: HttpError,
pub source: Option<Box<dyn StdError + Send + Sync>>,
}
Expand description
Wrapper for errors that are not a Fault. It is used as the explicit_error::Error::Domain variant generic type.
It is highly recommended to implement the derive HttpError which generates the boilerplate for your domain errors. Otherwise you can implement the ToDomainError trait.
Error implements From<DomainError>
, use ?
and .into()
in functions and closures to convert to the Error::Domain variant.
§Examples
DomainError can be generated because of a predicate
use explicit_error_http::{HttpError, Result, derive::HttpError};
#[derive(Debug, HttpError)]
enum MyError {
Domain,
}
impl From<&MyError> for HttpError {
fn from(value: &MyError) -> Self {
HttpError::new(
StatusCode::BAD_REQUEST,
"My domain error"
)
}
}
fn business_logic() -> Result<()> {
if 1 < 2 {
Err(MyError::Domain)?;
}
if true {
Err(HttpError::new(StatusCode::FORBIDDEN, "")
.with_context("Usefull context to debug or monitor"))?;
}
}
Or from a Result
use explicit_error_http::{Error, prelude::*, derive::HttpError, HttpError};
#[derive(HttpError, Debug)]
enum NotFoundError {
Bar(String)
}
impl From<&NotFoundError> for HttpError {
fn from(value: &NotFoundError) -> Self {
let (label, id) = match value {
NotFoundError::Bar(public_identifier) => ("Bar", public_identifier)
};
HttpError::new(
StatusCode::NOT_FOUND,
ProblemDetails::new()
.with_type(Uri::from_static("/errors/not-found"))
.with_title("Not found")
.with_detail(format!("Unknown {label} with identifier {id}."))
)
}
}
fn business_logic(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),
}
)?;
Ok(entity)
}
fn fetch_bar(public_identifier: &str) -> Result<(), sqlx::Error> {
Err(sqlx::Error::RowNotFound)
}
Or an Option
Some(12).ok_or(HttpError::new(StatusCode::FORBIDDEN, ""))?;
Fields§
§output: HttpError
§source: Option<Box<dyn StdError + Send + Sync>>
Trait Implementations§
Source§impl Debug for DomainError
impl Debug for DomainError
Source§impl Display for DomainError
impl Display for DomainError
Source§impl Domain for DomainError
impl Domain for DomainError
Source§impl Error for DomainError
impl Error for DomainError
Source§fn source(&self) -> Option<&(dyn StdError + 'static)>
fn source(&self) -> Option<&(dyn StdError + 'static)>
Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
👎Deprecated since 1.42.0: use the Display impl or to_string()
Source§impl From<DomainError> for Error<DomainError>
impl From<DomainError> for Error<DomainError>
Source§fn from(value: DomainError) -> Self
fn from(value: DomainError) -> Self
Converts to this type from the input type.
Auto Trait Implementations§
impl Freeze for DomainError
impl !RefUnwindSafe for DomainError
impl Send for DomainError
impl Sync for DomainError
impl Unpin for DomainError
impl !UnwindSafe for DomainError
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more