mirror of
https://github.com/fafhrd91/actix-web
synced 2025-01-19 06:04:40 +01:00
Improve module docs for error handler middleware (#2543)
This commit is contained in:
parent
01cbfc5724
commit
34e5c7c799
@ -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…
x
Reference in New Issue
Block a user