Struct actix_web::middleware::ErrorHandlers
source · pub struct ErrorHandlers<S> { /* private fields */ }
Expand description
Middleware
for allowing custom handlers for responses.
You can use ErrorHandlers::handler()
method to register a custom error
handler for specific status code. You can modify existing response or
create completely new one.
Example
use actix_web::middleware::{ErrorHandlers, Response};
use actix_web::{http, App, HttpRequest, HttpResponse, Result};
fn render_500<S>(_: &HttpRequest<S>, resp: HttpResponse) -> Result<Response> {
let mut builder = resp.into_builder();
builder.header(http::header::CONTENT_TYPE, "application/json");
Ok(Response::Done(builder.into()))
}
fn main() {
let app = App::new()
.middleware(
ErrorHandlers::new()
.handler(http::StatusCode::INTERNAL_SERVER_ERROR, render_500),
)
.resource("/test", |r| {
r.method(http::Method::GET).f(|_| HttpResponse::Ok());
r.method(http::Method::HEAD)
.f(|_| HttpResponse::MethodNotAllowed());
})
.finish();
}
Implementations
sourceimpl<S> ErrorHandlers<S>
impl<S> ErrorHandlers<S>
sourcepub fn handler<F>(self, status: StatusCode, handler: F) -> Selfwhere
F: Fn(&HttpRequest<S>, HttpResponse) -> Result<Response> + 'static,
pub fn handler<F>(self, status: StatusCode, handler: F) -> Selfwhere
F: Fn(&HttpRequest<S>, HttpResponse) -> Result<Response> + 'static,
Register error handler for specified status code
Trait Implementations
sourceimpl<S> Default for ErrorHandlers<S>
impl<S> Default for ErrorHandlers<S>
sourceimpl<S: 'static> Middleware<S> for ErrorHandlers<S>
impl<S: 'static> Middleware<S> for ErrorHandlers<S>
sourcefn response(&self, req: &HttpRequest<S>, resp: HttpResponse) -> Result<Response>
fn response(&self, req: &HttpRequest<S>, resp: HttpResponse) -> Result<Response>
Method is called when handler returns response,
but before sending http message to peer. Read more
sourcefn start(&self, req: &HttpRequest<S>) -> Result<Started>
fn start(&self, req: &HttpRequest<S>) -> Result<Started>
Method is called when request is ready. It may return
future, which should resolve before next middleware get called. Read more
sourcefn finish(&self, req: &HttpRequest<S>, resp: &HttpResponse) -> Finished
fn finish(&self, req: &HttpRequest<S>, resp: &HttpResponse) -> Finished
Method is called after body stream get sent to peer.
Auto Trait Implementations
impl<S> !RefUnwindSafe for ErrorHandlers<S>
impl<S> !Send for ErrorHandlers<S>
impl<S> !Sync for ErrorHandlers<S>
impl<S> Unpin for ErrorHandlers<S>
impl<S> !UnwindSafe for ErrorHandlers<S>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more