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
use std::{future::Future, marker::PhantomData};

use crate::{Endpoint, IntoResponse, Request, Response, Result};

/// Endpoint for the [`catch_error`](super::EndpointExt::catch_error) method.
pub struct CatchError<E, F, R, ErrType> {
    inner: E,
    f: F,
    _mark1: PhantomData<R>,
    _mark2: PhantomData<ErrType>,
}

impl<E, F, R, ErrType> CatchError<E, F, R, ErrType> {
    #[inline]
    pub(crate) fn new(inner: E, f: F) -> CatchError<E, F, R, ErrType> {
        Self {
            inner,
            f,
            _mark1: PhantomData,
            _mark2: PhantomData,
        }
    }
}

#[async_trait::async_trait]
impl<E, F, Fut, R, ErrType> Endpoint for CatchError<E, F, R, ErrType>
where
    E: Endpoint,
    F: Fn(ErrType) -> Fut + Send + Sync,
    Fut: Future<Output = R> + Send,
    R: IntoResponse + Send + Sync,
    ErrType: std::error::Error + Send + Sync + 'static,
{
    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) if err.is::<ErrType>() => Ok((self.f)(err.downcast::<ErrType>().unwrap())
                .await
                .into_response()),
            Err(err) => Err(err),
        }
    }
}