Struct actix_web_lab::middleware::ErrorHandlers
source · pub struct ErrorHandlers<B> { /* private fields */ }
Expand description
Middleware for registering custom status code based error handlers.
Register handlers with the ErrorHandlers::handler()
method to register a custom error handler
for a given status code. Handlers can modify existing responses or create completely new ones.
§Examples
use actix_web::{
body::EitherBody,
dev::ServiceResponse,
http::{header, StatusCode},
web, App, HttpResponse, Result,
};
use actix_web_lab::middleware::ErrorHandlers;
async fn add_error_header<B>(
mut res: ServiceResponse<B>,
) -> Result<ServiceResponse<EitherBody<B>>> {
res.response_mut().headers_mut().insert(
header::CONTENT_TYPE,
header::HeaderValue::from_static("Error"),
);
Ok(res.map_into_left_body())
}
let app = App::new()
.wrap(ErrorHandlers::new().handler(StatusCode::INTERNAL_SERVER_ERROR, add_error_header))
.service(web::resource("/").route(web::get().to(HttpResponse::InternalServerError)));
Implementations§
source§impl<B> ErrorHandlers<B>
impl<B> ErrorHandlers<B>
sourcepub fn handler<F, Fut>(self, status: StatusCode, handler: F) -> Selfwhere
F: Fn(ServiceResponse<B>) -> Fut + 'static,
Fut: Future<Output = Result<ServiceResponse<EitherBody<B>>>> + 'static,
pub fn handler<F, Fut>(self, status: StatusCode, handler: F) -> Selfwhere
F: Fn(ServiceResponse<B>) -> Fut + 'static,
Fut: Future<Output = Result<ServiceResponse<EitherBody<B>>>> + 'static,
Register error handler for specified status code.
Trait Implementations§
source§impl<B> Default for ErrorHandlers<B>
impl<B> Default for ErrorHandlers<B>
source§impl<S, B> Transform<S, ServiceRequest> for ErrorHandlers<B>where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
S::Future: 'static,
B: 'static,
impl<S, B> Transform<S, ServiceRequest> for ErrorHandlers<B>where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
S::Future: 'static,
B: 'static,
§type Response = ServiceResponse<EitherBody<B>>
type Response = ServiceResponse<EitherBody<B>>
Responses produced by the service.
§type Future = Pin<Box<dyn Future<Output = Result<<ErrorHandlers<B> as Transform<S, ServiceRequest>>::Transform, <ErrorHandlers<B> as Transform<S, ServiceRequest>>::InitError>>>>
type Future = Pin<Box<dyn Future<Output = Result<<ErrorHandlers<B> as Transform<S, ServiceRequest>>::Transform, <ErrorHandlers<B> as Transform<S, ServiceRequest>>::InitError>>>>
The future response value.
source§fn new_transform(&self, service: S) -> Self::Future
fn new_transform(&self, service: S) -> Self::Future
Creates and returns a new Transform component, asynchronously
Auto Trait Implementations§
impl<B> Freeze for ErrorHandlers<B>
impl<B> !RefUnwindSafe for ErrorHandlers<B>
impl<B> !Send for ErrorHandlers<B>
impl<B> !Sync for ErrorHandlers<B>
impl<B> Unpin for ErrorHandlers<B>
impl<B> !UnwindSafe for ErrorHandlers<B>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more