1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use std::{
    future::{ready, Ready},
    panic::AssertUnwindSafe,
    rc::Rc,
};

use actix_web::{
    dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform},
    error,
};
use futures_core::future::LocalBoxFuture;
use futures_util::FutureExt as _;

/// A middleware to catch panics in wrapped handlers and middleware, returning empty 500 responses.
///
/// **This middleware should never be used as replacement for proper error handling.** See [this
/// thread](https://github.com/actix/actix-web/issues/1501#issuecomment-627517783) for historical
/// discussion on why Actix Web does not do this by default.
///
/// It is recommended that this middleware be registered last. That is, `wrap`ed after everything
/// else except `Logger`.
///
/// # Examples
/// ```
/// # use actix_web::App;
/// use actix_web_lab::middleware::CatchPanic;
///
/// App::new()
///     .wrap(CatchPanic::default())
///     # ;
/// ```
///
/// ```ignore
/// // recommended wrap order
/// App::new()
///     .wrap(NormalizePath::default())
///     .wrap(CatchPanic::default()) // <- after everything except logger
///     .wrap(Logger::default())
///     # ;
/// ```
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct CatchPanic;

impl<S, B> Transform<S, ServiceRequest> for CatchPanic
where
    S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = actix_web::Error> + 'static,
{
    type Response = ServiceResponse<B>;
    type Error = actix_web::Error;
    type Transform = CatchPanicMiddleware<S>;
    type InitError = ();
    type Future = Ready<Result<Self::Transform, Self::InitError>>;

    fn new_transform(&self, service: S) -> Self::Future {
        ready(Ok(CatchPanicMiddleware {
            service: Rc::new(service),
        }))
    }
}

pub struct CatchPanicMiddleware<S> {
    service: Rc<S>,
}

impl<S, B> Service<ServiceRequest> for CatchPanicMiddleware<S>
where
    S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = actix_web::Error> + 'static,
{
    type Response = ServiceResponse<B>;
    type Error = actix_web::Error;
    type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;

    forward_ready!(service);

    fn call(&self, req: ServiceRequest) -> Self::Future {
        AssertUnwindSafe(self.service.call(req))
            .catch_unwind()
            .map(move |res| match res {
                Ok(Ok(res)) => Ok(res),
                Ok(Err(svc_err)) => Err(svc_err),
                Err(_panic_err) => Err(error::ErrorInternalServerError("")),
            })
            .boxed_local()
    }
}

#[cfg(test)]
mod tests {
    use actix_web::{
        body::{to_bytes, MessageBody},
        dev::{Service as _, ServiceFactory},
        http::StatusCode,
        test, web, App, Error,
    };

    use super::*;

    fn test_app() -> App<
        impl ServiceFactory<
            ServiceRequest,
            Response = ServiceResponse<impl MessageBody>,
            Config = (),
            InitError = (),
            Error = Error,
        >,
    > {
        App::new()
            .wrap(CatchPanic::default())
            .route("/", web::get().to(|| async { "content" }))
            .route(
                "/disco",
                #[allow(unreachable_code)]
                web::get().to(|| async {
                    panic!("the disco");
                    ""
                }),
            )
    }

    #[actix_web::test]
    async fn pass_through_no_panic() {
        let app = test::init_service(test_app()).await;

        let req = test::TestRequest::default().to_request();
        let res = test::call_service(&app, req).await;
        assert_eq!(res.status(), StatusCode::OK);
        let body = test::read_body(res).await;
        assert_eq!(body, "content");
    }

    #[actix_web::test]
    async fn catch_panic_return_internal_server_error_response() {
        let app = test::init_service(test_app()).await;

        let req = test::TestRequest::with_uri("/disco").to_request();
        let err = match app.call(req).await {
            Ok(_) => panic!("unexpected Ok response"),
            Err(err) => err,
        };
        let res = err.error_response();
        assert_eq!(res.status(), StatusCode::INTERNAL_SERVER_ERROR);
        let body = to_bytes(res.into_body()).await.unwrap();
        assert!(body.is_empty());
    }
}