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
use std::{future::Future, marker::PhantomData};
use crate::{Endpoint, Error, IntoResponse, Request, Response, Result};
pub struct CatchAllError<E, F, R> {
inner: E,
f: F,
_mark: PhantomData<R>,
}
impl<E, F, R> CatchAllError<E, F, R> {
#[inline]
pub(crate) fn new(inner: E, f: F) -> CatchAllError<E, F, R> {
Self {
inner,
f,
_mark: PhantomData,
}
}
}
#[async_trait::async_trait]
impl<E, F, Fut, R> Endpoint for CatchAllError<E, F, R>
where
E: Endpoint,
F: Fn(Error) -> Fut + Send + Sync,
Fut: Future<Output = R> + Send,
R: IntoResponse + Send + Sync,
{
type Output = Response;
async fn call(&self, req: Request) -> Result<Self::Output> {
match self.inner.call(req).await {
Ok(resp) => Ok(resp.into_response()),
Err(err) => Ok((self.f)(err).await.into_response()),
}
}
}