modo/middleware/
catch_panic.rs1use std::any::Any;
2
3use axum::response::{IntoResponse, Response};
4use http::StatusCode;
5use tower_http::catch_panic::CatchPanicLayer;
6
7#[derive(Clone)]
13pub struct ModoPanicHandler;
14
15impl tower_http::catch_panic::ResponseForPanic for ModoPanicHandler {
16 type ResponseBody = axum::body::Body;
17
18 fn response_for_panic(
19 &mut self,
20 _err: Box<dyn Any + Send + 'static>,
21 ) -> Response<Self::ResponseBody> {
22 let error = crate::error::Error::internal("internal server error");
23 let mut response = StatusCode::INTERNAL_SERVER_ERROR.into_response();
24 response.extensions_mut().insert(error);
25 response
26 }
27}
28
29pub fn catch_panic() -> CatchPanicLayer<ModoPanicHandler> {
34 CatchPanicLayer::custom(ModoPanicHandler)
35}