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,
http::{header, StatusCode},
dev::ServiceResponse,
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, BoxBody>>
type Response = ServiceResponse<EitherBody<B, BoxBody>>
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>>, Global>>
type Future = Pin<Box<dyn Future<Output = Result<<ErrorHandlers<B> as Transform<S, ServiceRequest>>::Transform, <ErrorHandlers<B> as Transform<S, ServiceRequest>>::InitError>>, Global>>
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> !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