relay_server/middlewares/
handle_panic.rs

1use std::any::Any;
2
3use axum::http::StatusCode;
4use axum::response::{IntoResponse, Response};
5pub use tower_http::catch_panic::CatchPanicLayer;
6
7use crate::utils::ApiErrorResponse;
8
9/// Handler function for the [`CatchPanicLayer`] middleware.
10pub fn handle_panic(err: Box<dyn Any + Send + 'static>) -> Response {
11    let detail = if let Some(s) = err.downcast_ref::<String>() {
12        s.as_str()
13    } else if let Some(s) = err.downcast_ref::<&str>() {
14        s
15    } else {
16        "no error details"
17    };
18
19    relay_log::error!("panic in web handler: {detail}");
20
21    let response = (
22        StatusCode::INTERNAL_SERVER_ERROR,
23        ApiErrorResponse::with_detail(detail),
24    );
25
26    response.into_response()
27}