Catch Panic
Catch Panic is used to capture crashes that occur when the program handles requests during operation. For specific APIs, please see the documentation.
Sample code
use salvo::prelude::*;
#[handler]
async fn hello() {
panic!("panic error!");
}
#[tokio::main]
async fn main() {
tracing_subscriber::fmt().init();
let router = Router::new().hoop(CatchPanic::new()).get(hello);
let acceptor = TcpListener::new("0.0.0.0:5800").bind().await;
Server::new(acceptor).serve(router).await;
}
[package]
name = "example-catch-panic"
version.workspace = true
edition.workspace = true
publish.workspace = true
[dependencies]
anyhow = "1"
salvo = { workspace = true, features=["catch-panic"] }
tokio = { workspace = true, features = ["macros"] }
tracing.workspace = true
tracing-subscriber.workspace = true