pub trait HandlerError{
// Required methods
fn error(&self) -> &Error;
fn public_fault_response(fault: &Fault) -> impl Serialize;
fn domain_response(error: &DomainError) -> impl Serialize;
fn from_error(value: Error) -> Self;
}
Expand description
The type Error cannot directly be used as handlers or middlewares returned Err variant. A dedicated type is required. The easiest implementation is to declare a Newtype, derive it with the HandlerErrorHelpers and implement the HandlerError trait.
#[derive(HandlerErrorHelpers)]
struct MyHandlerError(Error);
impl HandlerError for MyHandlerError {
// Used by the derive for conversion
fn from_error(value: Error) -> Self {
MyHandlerError(value)
}
// Set-up monitoring and your custom HTTP response body for faults
fn public_fault_response(fault: &Fault) -> impl Serialize {
#[cfg(debug_assertions)]
error!("{fault}");
#[cfg(not(debug_assertions))]
error!("{}", serde_json::json!(fault));
ProblemDetails::new()
.with_type(http::Uri::from_static("/errors/internal-server-error"))
.with_title("Internal server error")
}
fn error(&self) -> &Error {
&self.0
}
// Monitor domain variant of your errors and eventually override their body
fn domain_response(error: &explicit_error_http::DomainError) -> impl Serialize {
if error.output.http_status_code.as_u16() < 500 {
debug!("{error}");
} else {
error!("{error}");
}
error
}
}
#[get("/my-handler")]
async fn my_handler() -> Result<HttpResponse, MyHandlerError> {
Ok(HttpResponse::Ok().finish())
}
Required Methods§
Sourcefn error(&self) -> &Error
fn error(&self) -> &Error
Accessor required by HandlerErrorHelpers
Sourcefn public_fault_response(fault: &Fault) -> impl Serialize
fn public_fault_response(fault: &Fault) -> impl Serialize
Set-up monitoring and your custom HTTP response body for faults
§Examples
fn public_fault_response(fault: &Fault) -> impl Serialize {
#[cfg(debug_assertions)]
error!("{fault}");
#[cfg(not(debug_assertions))]
error!("{}", serde_json::json!(fault));
ProblemDetails::new()
.with_type(http::Uri::from_static("/errors/internal-server-error"))
.with_title("Internal server error")
}
Sourcefn domain_response(error: &DomainError) -> impl Serialize
fn domain_response(error: &DomainError) -> impl Serialize
Monitor domain variant of your errors and eventually override their body
§Examples
fn domain_response(error: &explicit_error_http::DomainError) -> impl Serialize {
if error.output.http_status_code.as_u16() < 500 {
debug!("{error}");
} else {
error!("{error}");
}
error
}
Sourcefn from_error(value: Error) -> Self
fn from_error(value: Error) -> Self
Used by the derive for conversion
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.