Catch Panic
Catch Panic 用于捕获运行过程中程序处理请求时出现的奔溃. 具体 API 请查看文档.
示例代码
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