Module salvo_core::catcher
source · Expand description
Catch and handle errors.
If the status code of Response
is an error, and the body of Response
is empty, then salvo
will try to use Catcher
to catch the error and display a friendly error page.
You can return a system default Catcher
through Catcher::default()
, and then add it to Service
:
§Example
use salvo_core::prelude::*;
use salvo_core::catcher::Catcher;
#[handler]
async fn handle404(&self, res: &mut Response, ctrl: &mut FlowCtrl) {
if let Some(StatusCode::NOT_FOUND) = res.status_code {
res.render("Custom 404 Error Page");
ctrl.skip_rest();
}
}
#[tokio::main]
async fn main() {
Service::new(Router::new()).catcher(Catcher::default().hoop(handle404));
}
The default Catcher
supports sending error pages in XML
, JSON
, HTML
, Text
formats.
You can add a custom error handler to Catcher
by adding hoop
to the default Catcher
.
The error handler is still Handler
.
You can add multiple custom error catching handlers to Catcher
through Catcher::hoop
. The custom error handler can call
the FlowCtrl::skip_rest
method after handling the error to skip next error handlers and return early.
Structs§
Catcher
is used to catch errors.