rama_http/layer/error_handling.rs
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
//! Middleware to turn [`Service`] errors into [`Response`]s.
//!
//! # Example
//!
//! ```
//! use rama_core::{
//! service::service_fn,
//! Context, Service, Layer,
//! };
//! use rama_http::{
//! service::client::HttpClientExt,
//! layer::{error_handling::ErrorHandlerLayer, timeout::TimeoutLayer},
//! service::web::WebService,
//! Body, IntoResponse, Request, Response, StatusCode,
//! };
//! use std::time::Duration;
//!
//! # async fn some_expensive_io_operation() -> Result<(), std::io::Error> {
//! # Ok(())
//! # }
//!
//! async fn handler<S>(_ctx: Context<S>, _req: Request) -> Result<Response, std::io::Error> {
//! some_expensive_io_operation().await?;
//! Ok(StatusCode::OK.into_response())
//! }
//!
//! # #[tokio::main]
//! # async fn main() {
//! let home_handler = (
//! ErrorHandlerLayer::new().error_mapper(|err| {
//! tracing::error!("Error: {:?}", err);
//! StatusCode::INTERNAL_SERVER_ERROR.into_response()
//! }),
//! TimeoutLayer::new(Duration::from_secs(5)),
//! ).layer(service_fn(handler));
//!
//! let service = WebService::default().get("/", home_handler);
//!
//! let _ = service.serve(Context::default(), Request::builder()
//! .method("GET")
//! .uri("/")
//! .body(Body::empty())
//! .unwrap()).await;
//! # }
//! ```
use crate::{IntoResponse, Request, Response};
use rama_core::{Context, Layer, Service};
use rama_utils::macros::define_inner_service_accessors;
use std::{convert::Infallible, fmt};
/// A [`Layer`] that wraps a [`Service`] and converts errors into [`Response`]s.
pub struct ErrorHandlerLayer<F = ()> {
error_mapper: F,
}
impl Default for ErrorHandlerLayer {
fn default() -> Self {
Self::new()
}
}
impl<F: fmt::Debug> fmt::Debug for ErrorHandlerLayer<F> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("ErrorHandlerLayer")
.field("error_mapper", &self.error_mapper)
.finish()
}
}
impl<F: Clone> Clone for ErrorHandlerLayer<F> {
fn clone(&self) -> Self {
Self {
error_mapper: self.error_mapper.clone(),
}
}
}
impl ErrorHandlerLayer {
/// Create a new [`ErrorHandlerLayer`].
pub const fn new() -> Self {
Self { error_mapper: () }
}
/// Set the error mapper function (not set by default).
///
/// The error mapper function is called with the error,
/// and should return an [`IntoResponse`] implementation.
pub fn error_mapper<F>(self, error_mapper: F) -> ErrorHandlerLayer<F> {
ErrorHandlerLayer { error_mapper }
}
}
impl<S, F: Clone> Layer<S> for ErrorHandlerLayer<F> {
type Service = ErrorHandler<S, F>;
fn layer(&self, inner: S) -> Self::Service {
ErrorHandler::new(inner).error_mapper(self.error_mapper.clone())
}
}
/// A [`Service`] adapter that handles errors by converting them into [`Response`]s.
pub struct ErrorHandler<S, F = ()> {
inner: S,
error_mapper: F,
}
impl<S: fmt::Debug, F: fmt::Debug> fmt::Debug for ErrorHandler<S, F> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("ErrorHandler")
.field("inner", &self.inner)
.field("error_mapper", &self.error_mapper)
.finish()
}
}
impl<S: Clone, F: Clone> Clone for ErrorHandler<S, F> {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
error_mapper: self.error_mapper.clone(),
}
}
}
impl<S> ErrorHandler<S> {
/// Create a new [`ErrorHandler`] wrapping the given service.
pub const fn new(inner: S) -> Self {
Self {
inner,
error_mapper: (),
}
}
define_inner_service_accessors!();
/// Set the error mapper function (not set by default).
///
/// The error mapper function is called with the error,
/// and should return an [`IntoResponse`] implementation.
pub fn error_mapper<F>(self, error_mapper: F) -> ErrorHandler<S, F> {
ErrorHandler {
inner: self.inner,
error_mapper,
}
}
}
impl<S, State, Body> Service<State, Request<Body>> for ErrorHandler<S, ()>
where
S: Service<State, Request<Body>, Response: IntoResponse, Error: IntoResponse>,
State: Clone + Send + Sync + 'static,
Body: Send + 'static,
{
type Response = Response;
type Error = Infallible;
async fn serve(
&self,
ctx: Context<State>,
req: Request<Body>,
) -> Result<Self::Response, Self::Error> {
match self.inner.serve(ctx, req).await {
Ok(response) => Ok(response.into_response()),
Err(error) => Ok(error.into_response()),
}
}
}
impl<S, F, R, State, Body> Service<State, Request<Body>> for ErrorHandler<S, F>
where
S: Service<State, Request<Body>, Response: IntoResponse>,
F: Fn(S::Error) -> R + Clone + Send + Sync + 'static,
R: IntoResponse + 'static,
State: Clone + Send + Sync + 'static,
Body: Send + 'static,
{
type Response = Response;
type Error = Infallible;
async fn serve(
&self,
ctx: Context<State>,
req: Request<Body>,
) -> Result<Self::Response, Self::Error> {
match self.inner.serve(ctx, req).await {
Ok(response) => Ok(response.into_response()),
Err(error) => Ok((self.error_mapper)(error).into_response()),
}
}
}