use crate::service::{Layer, Service};
use conjure_error::Error;
use std::error;
use std::fmt;
#[derive(Debug)]
pub struct RawClientError(pub Box<dyn error::Error + Sync + Send>);
impl fmt::Display for RawClientError {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.write_str("raw HTTP client error")
}
}
impl error::Error for RawClientError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
Some(&*self.0)
}
}
pub struct MapErrorLayer;
impl<S> Layer<S> for MapErrorLayer {
type Service = MapErrorService<S>;
fn layer(self, inner: S) -> MapErrorService<S> {
MapErrorService { inner }
}
}
pub struct MapErrorService<S> {
inner: S,
}
impl<S, R> Service<R> for MapErrorService<S>
where
S: Service<R>,
S::Error: Into<Box<dyn error::Error + Sync + Send>>,
{
type Response = S::Response;
type Error = Error;
async fn call(&self, req: R) -> Result<Self::Response, Self::Error> {
self.inner
.call(req)
.await
.map_err(|e| Error::internal_safe(RawClientError(e.into())))
}
}