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:
Mark Lodato 2021-12-22 13:59:09 -05:00
parent 7b1512d863
commit c00590497c
1 changed files with 10 additions and 16 deletions

View File

@ -37,27 +37,21 @@ type ErrorHandler<B> = dyn Fn(ServiceResponse<B>) -> Result<ErrorHandlerResponse
///
/// # Examples
/// ```
/// use actix_web::middleware::{ErrorHandlers, ErrorHandlerResponse};
/// use actix_web::{web, dev, App, HttpRequest, HttpResponse, Result};
/// use actix_web::http::{StatusCode, header};
///
/// 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"));
/// use actix_web::http::{header, StatusCode};
/// use actix_web::middleware::{ErrorHandlerResponse, ErrorHandlers};
/// use actix_web::{dev, web, App, HttpResponse, Result};
///
/// 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()))
/// }
///
/// let app = App::new()
/// .wrap(
/// ErrorHandlers::new()
/// .handler(StatusCode::INTERNAL_SERVER_ERROR, render_500),
/// )
/// .service(web::resource("/test")
/// .route(web::get().to(|| HttpResponse::Ok()))
/// .route(web::head().to(|| HttpResponse::MethodNotAllowed())
/// ));
/// .wrap(ErrorHandlers::new().handler(StatusCode::INTERNAL_SERVER_ERROR, add_error_header))
/// .service(web::resource("/").route(web::get().to(HttpResponse::InternalServerError)));
/// ```
pub struct ErrorHandlers<B> {
handlers: Handlers<B>,