Struct cosworth::prelude::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§
source§impl<S> ErrorHandlers<S>
impl<S> ErrorHandlers<S>
sourcepub fn new() -> ErrorHandlers<S>
pub fn new() -> ErrorHandlers<S>
Construct new ErrorHandlers
instance
sourcepub fn handler<F>(self, status: StatusCode, handler: F) -> ErrorHandlers<S>where
F: 'static + Fn(&HttpRequest<S>, HttpResponse) -> Result<Response, Error>,
pub fn handler<F>(self, status: StatusCode, handler: F) -> ErrorHandlers<S>where
F: 'static + Fn(&HttpRequest<S>, HttpResponse) -> Result<Response, Error>,
Register error handler for specified status code
Trait Implementations§
source§impl<S> Default for ErrorHandlers<S>
impl<S> Default for ErrorHandlers<S>
source§fn default() -> ErrorHandlers<S>
fn default() -> ErrorHandlers<S>
Returns the “default value” for a type. Read more
source§impl<S> Middleware<S> for ErrorHandlers<S>where
S: 'static,
impl<S> Middleware<S> for ErrorHandlers<S>where
S: 'static,
source§fn response(
&self,
req: &HttpRequest<S>,
resp: HttpResponse
) -> Result<Response, Error>
fn response(
&self,
req: &HttpRequest<S>,
resp: HttpResponse
) -> Result<Response, Error>
Method is called when handler returns response,
but before sending http message to peer. Read more
source§fn start(&self, req: &HttpRequest<S>) -> Result<Started, Error>
fn start(&self, req: &HttpRequest<S>) -> Result<Started, Error>
Method is called when request is ready. It may return
future, which should resolve before next middleware get called. Read more
source§fn 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§
source§impl<T> IntoSql for T
impl<T> IntoSql for T
source§fn into_sql<T>(self) -> Self::Expressionwhere
Self: AsExpression<T> + Sized,
fn into_sql<T>(self) -> Self::Expressionwhere
Self: AsExpression<T> + Sized,
Convert
self
to an expression for Diesel’s query builder. Read moresource§fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expressionwhere
&'a Self: AsExpression<T>,
fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expressionwhere
&'a Self: AsExpression<T>,
Convert
&self
to an expression for Diesel’s query builder. Read more