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
use std::marker::PhantomData;
use crate::{async_trait, Handler, IntoResponse, Response, Result};
#[derive(Debug)]
pub struct CatchError<H, F, R, E> {
h: H,
f: F,
_maker: PhantomData<(R, E)>,
}
impl<H, F, R, E> Clone for CatchError<H, F, R, E>
where
H: Clone,
F: Clone,
{
fn clone(&self) -> Self {
Self {
h: self.h.clone(),
f: self.f.clone(),
_maker: PhantomData,
}
}
}
impl<H, F, R, E> CatchError<H, F, R, E> {
#[inline]
pub(super) fn new(h: H, f: F) -> Self {
Self {
h,
f,
_maker: PhantomData,
}
}
}
#[async_trait]
impl<H, F, I, O, R, E> Handler<I> for CatchError<H, F, E, R>
where
I: Send + 'static,
O: IntoResponse + Send,
H: Handler<I, Output = Result<O>> + Clone,
F: Handler<E, Output = R> + Clone,
R: IntoResponse + Send + Sync + 'static,
E: std::error::Error + Send + Sync + 'static,
{
type Output = Result<Response>;
async fn call(&self, i: I) -> Self::Output {
match self.h.call(i).await {
Ok(r) => Ok(r.into_response()),
Err(e) if e.is::<E>() => Ok(self
.f
.call(e.downcast::<E>().unwrap())
.await
.into_response()),
Err(e) => Err(e),
}
}
}