use {
axum_core::response::{IntoResponse, Response},
http::StatusCode,
std::convert::Infallible,
};
#[derive(Debug)]
pub enum Error<U = Infallible> {
Extract,
Jwt(jsonwebtoken::errors::Error),
Custom(U),
}
impl<U> Error<U> {
pub fn map<F, E>(self, f: F) -> Error<E>
where
F: FnOnce(U) -> E,
{
match self {
Self::Extract => Error::Extract,
Self::Jwt(e) => Error::Jwt(e),
Self::Custom(u) => Error::Custom(f(u)),
}
}
}
impl<U> IntoResponse for Error<U>
where
U: IntoResponse,
{
fn into_response(self) -> Response {
match self {
Self::Extract | Self::Jwt(_) => StatusCode::UNAUTHORIZED.into_response(),
Self::Custom(u) => u.into_response(),
}
}
}