mirror of https://github.com/fafhrd91/actix-web
Improve module docs for error handler middleware
**This Commit** Updates the docs in the error handler middleware to match [changes made to the website][website]. **Why?** The previous documentation, when run, doesn't give the user a trivial way to exercise the middleware because none of the routes return a status code covered by the error handler middleware. There are also additional routes that don't seem obviously instructive and so may confuse a user into thinking error handlers only work with certain kinds of requests. [website]: https://github.com/actix/actix-website/pull/248
This commit is contained in:
parent
7b1512d863
commit
c00590497c
|
@ -37,27 +37,21 @@ type ErrorHandler<B> = dyn Fn(ServiceResponse<B>) -> Result<ErrorHandlerResponse
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
/// ```
|
/// ```
|
||||||
/// use actix_web::middleware::{ErrorHandlers, ErrorHandlerResponse};
|
/// use actix_web::http::{header, StatusCode};
|
||||||
/// use actix_web::{web, dev, App, HttpRequest, HttpResponse, Result};
|
/// use actix_web::middleware::{ErrorHandlerResponse, ErrorHandlers};
|
||||||
/// use actix_web::http::{StatusCode, header};
|
/// use actix_web::{dev, web, App, HttpResponse, Result};
|
||||||
///
|
|
||||||
/// fn render_500<B>(mut res: dev::ServiceResponse<B>) -> Result<ErrorHandlerResponse<B>> {
|
|
||||||
/// res.response_mut()
|
|
||||||
/// .headers_mut()
|
|
||||||
/// .insert(header::CONTENT_TYPE, header::HeaderValue::from_static("Error"));
|
|
||||||
///
|
///
|
||||||
|
/// fn add_error_header<B>(mut res: dev::ServiceResponse<B>) -> Result<ErrorHandlerResponse<B>> {
|
||||||
|
/// res.response_mut().headers_mut().insert(
|
||||||
|
/// header::CONTENT_TYPE,
|
||||||
|
/// header::HeaderValue::from_static("Error"),
|
||||||
|
/// );
|
||||||
/// Ok(ErrorHandlerResponse::Response(res.map_into_left_body()))
|
/// Ok(ErrorHandlerResponse::Response(res.map_into_left_body()))
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// let app = App::new()
|
/// let app = App::new()
|
||||||
/// .wrap(
|
/// .wrap(ErrorHandlers::new().handler(StatusCode::INTERNAL_SERVER_ERROR, add_error_header))
|
||||||
/// ErrorHandlers::new()
|
/// .service(web::resource("/").route(web::get().to(HttpResponse::InternalServerError)));
|
||||||
/// .handler(StatusCode::INTERNAL_SERVER_ERROR, render_500),
|
|
||||||
/// )
|
|
||||||
/// .service(web::resource("/test")
|
|
||||||
/// .route(web::get().to(|| HttpResponse::Ok()))
|
|
||||||
/// .route(web::head().to(|| HttpResponse::MethodNotAllowed())
|
|
||||||
/// ));
|
|
||||||
/// ```
|
/// ```
|
||||||
pub struct ErrorHandlers<B> {
|
pub struct ErrorHandlers<B> {
|
||||||
handlers: Handlers<B>,
|
handlers: Handlers<B>,
|
||||||
|
|
Loading…
Reference in New Issue