pub trait HandlerError {
// Required methods
fn http_error(&self) -> &Error;
fn public_bug_response(bug: &Bug) -> impl Serialize;
fn on_domain_response(error: &DomainError);
fn from_http_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 HandlerError and implement the HandlerError trait.
#[derive(HandlerError)]
struct MyHandlerError(Error);
impl HandlerError for MyHandlerError {
// Used by the derive for conversion
fn from_http_error(value: Error) -> Self {
MyHandlerError(value)
}
// Set-up monitoring and your custom HTTP response body for bugs
fn public_bug_response(bug: &Bug) -> impl Serialize {
#[cfg(debug_assertions)]
error!("{bug}");
#[cfg(not(debug_assertions))]
error!("{}", serde_json::json!(bug));
ProblemDetails::new()
.with_type(http::Uri::from_static("/errors/internal-server-error"))
.with_title("Internal server error")
}
fn http_error(&self) -> &Error {
&self.0
}
// Monitor domain variant of your errors
fn on_domain_response(error: &explicit_error_http::DomainError) {
if error.output.http_status_code.as_u16() < 500 {
debug!("{error}");
} else {
error!("{error}");
}
}
}
#[get("/my-handler")]
async fn my_handler() -> Result<HttpResponse, MyHandlerError> {
Ok(HttpResponse::Ok().finish())
}Required Methods§
Sourcefn http_error(&self) -> &Error
fn http_error(&self) -> &Error
Accessor required by HandlerError
Sourcefn public_bug_response(bug: &Bug) -> impl Serialize
fn public_bug_response(bug: &Bug) -> impl Serialize
Set-up monitoring and your custom HTTP response body for bugs
§Examples
fn public_bug_response(bug: &Bug) -> impl Serialize {
#[cfg(debug_assertions)]
error!("{bug}");
#[cfg(not(debug_assertions))]
error!("{}", serde_json::json!(bug));
ProblemDetails::new()
.with_type(http::Uri::from_static("/errors/internal-server-error"))
.with_title("Internal server error")
}Sourcefn on_domain_response(error: &DomainError)
fn on_domain_response(error: &DomainError)
Monitor domain variant of your errors
§Examples
fn on_domain_response(error: &explicit_error_http::DomainError) {
if error.output.http_status_code.as_u16() < 500 {
debug!("{error}");
} else {
error!("{error}");
}
}Sourcefn from_http_error(value: Error) -> Self
fn from_http_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.