viz_core/handler/
catch_unwind.rs1use crate::{Handler, IntoResponse, Response, Result, future::FutureExt};
2
3#[derive(Clone, Debug)]
5pub struct CatchUnwind<H, F> {
6 h: H,
7 f: F,
8}
9
10impl<H, F> CatchUnwind<H, F> {
11 #[inline]
13 pub const fn new(h: H, f: F) -> Self {
14 Self { h, f }
15 }
16}
17
18#[crate::async_trait]
19impl<H, F, I, O, R> Handler<I> for CatchUnwind<H, F>
20where
21 I: Send + 'static,
22 H: Handler<I, Output = Result<O>>,
23 O: IntoResponse + Send,
24 F: Handler<Box<dyn ::core::any::Any + Send>, Output = R>,
25 R: IntoResponse,
26{
27 type Output = Result<Response>;
28
29 async fn call(&self, i: I) -> Self::Output {
30 match ::core::panic::AssertUnwindSafe(self.h.call(i))
31 .catch_unwind()
32 .await
33 {
34 Ok(r) => r.map(IntoResponse::into_response),
35 Err(e) => Ok(self.f.call(e).await.into_response()),
36 }
37 }
38}