#![expect(
clippy::allow_attributes,
reason = "macro-generated `#[allow]` attributes whose underlying lints fire only for some expansions"
)]
use crate::{Body, HeaderValue, Request, Response, StatusCode};
use rama_core::futures::FutureExt;
use rama_core::telemetry::tracing;
use rama_core::{Layer, Service};
use rama_utils::macros::define_inner_service_accessors;
use std::{any::Any, panic::AssertUnwindSafe};
#[derive(Debug, Clone)]
pub struct CatchPanicLayer<T> {
panic_handler: T,
}
impl Default for CatchPanicLayer<DefaultResponseForPanic> {
fn default() -> Self {
Self::new()
}
}
impl CatchPanicLayer<DefaultResponseForPanic> {
#[must_use]
pub const fn new() -> Self {
Self {
panic_handler: DefaultResponseForPanic,
}
}
}
impl<T> CatchPanicLayer<T> {
pub fn custom(panic_handler: T) -> Self
where
T: ResponseForPanic,
{
Self { panic_handler }
}
}
impl<T, S> Layer<S> for CatchPanicLayer<T>
where
T: Clone,
{
type Service = CatchPanic<S, T>;
fn layer(&self, inner: S) -> Self::Service {
CatchPanic {
inner,
panic_handler: self.panic_handler.clone(),
}
}
fn into_layer(self, inner: S) -> Self::Service {
CatchPanic {
inner,
panic_handler: self.panic_handler,
}
}
}
#[derive(Debug, Clone)]
pub struct CatchPanic<S, T> {
inner: S,
panic_handler: T,
}
impl<S> CatchPanic<S, DefaultResponseForPanic> {
pub const fn new(inner: S) -> Self {
Self {
inner,
panic_handler: DefaultResponseForPanic,
}
}
}
impl<S, T> CatchPanic<S, T> {
define_inner_service_accessors!();
pub const fn custom(inner: S, panic_handler: T) -> Self
where
T: ResponseForPanic,
{
Self {
inner,
panic_handler,
}
}
}
impl<S, T, ReqBody, ResBody> Service<Request<ReqBody>> for CatchPanic<S, T>
where
S: Service<Request<ReqBody>, Output = Response<ResBody>>,
ResBody: Into<Body> + Send + 'static,
T: ResponseForPanic + Clone + Send + Sync + 'static,
ReqBody: Send + 'static,
ResBody: Send + 'static,
{
type Output = Response;
type Error = S::Error;
async fn serve(&self, req: Request<ReqBody>) -> Result<Self::Output, Self::Error> {
let future = match std::panic::catch_unwind(AssertUnwindSafe(|| self.inner.serve(req))) {
Ok(future) => future,
Err(panic_err) => return Ok(self.panic_handler.response_for_panic(panic_err)),
};
match AssertUnwindSafe(future).catch_unwind().await {
Ok(res) => match res {
Ok(res) => Ok(res.map(Into::into)),
Err(err) => Err(err),
},
Err(panic_err) => Ok(self.panic_handler.response_for_panic(panic_err)),
}
}
}
pub trait ResponseForPanic: Clone {
fn response_for_panic(&self, err: Box<dyn Any + Send + 'static>) -> Response<Body>;
}
impl<F> ResponseForPanic for F
where
F: Fn(Box<dyn Any + Send + 'static>) -> Response + Clone,
{
fn response_for_panic(&self, err: Box<dyn Any + Send + 'static>) -> Response {
self(err)
}
}
#[derive(Debug, Default, Clone)]
#[non_exhaustive]
pub struct DefaultResponseForPanic;
impl ResponseForPanic for DefaultResponseForPanic {
fn response_for_panic(&self, err: Box<dyn Any + Send + 'static>) -> Response {
if let Some(s) = err.downcast_ref::<String>() {
tracing::error!("Service panicked: {}", s);
} else if let Some(s) = err.downcast_ref::<&str>() {
tracing::error!("Service panicked: {}", s);
} else {
tracing::error!(
"Service panicked but `CatchPanic` was unable to downcast the panic info"
);
};
let mut res = Response::new(Body::from("Service panicked"));
*res.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
#[allow(clippy::declare_interior_mutable_const)]
const TEXT_PLAIN: HeaderValue = HeaderValue::from_static("text/plain; charset=utf-8");
res.headers_mut()
.insert(rama_http_types::header::CONTENT_TYPE, TEXT_PLAIN);
res
}
}
#[cfg(test)]
mod tests {
#![allow(unreachable_code)]
use super::*;
use crate::{Body, Response, body::util::BodyExt};
use rama_core::Service;
use rama_core::service::service_fn;
use std::convert::Infallible;
#[tokio::test]
async fn panic_before_returning_future() {
let svc = CatchPanicLayer::new().into_layer(service_fn(|_: Request| {
panic!("service panic");
async { Ok::<_, Infallible>(Response::new(Body::empty())) }
}));
let req = Request::new(Body::empty());
let res = svc.serve(req).await.unwrap();
assert_eq!(res.status(), StatusCode::INTERNAL_SERVER_ERROR);
let body = res.into_body().collect().await.unwrap().to_bytes();
assert_eq!(&body[..], b"Service panicked");
}
#[tokio::test]
async fn panic_in_future() {
let svc = CatchPanicLayer::new().into_layer(service_fn(async |_: Request<Body>| {
panic!("future panic");
Ok::<_, Infallible>(Response::new(Body::empty()))
}));
let req = Request::new(Body::empty());
let res = svc.serve(req).await.unwrap();
assert_eq!(res.status(), StatusCode::INTERNAL_SERVER_ERROR);
let body = res.into_body().collect().await.unwrap().to_bytes();
assert_eq!(&body[..], b"Service panicked");
}
}