mirror of
https://github.com/actix/actix-web.git
synced 2024-11-26 03:21:08 +00:00
added body manipulation example for error handlers (#2973)
Closes https://github.com/actix/actix-web/issues/2856
This commit is contained in:
parent
bf1f169be2
commit
bf19a0e761
1 changed files with 24 additions and 0 deletions
|
@ -50,6 +50,8 @@ type DefaultHandler<B> = Option<Rc<ErrorHandler<B>>>;
|
|||
/// will pass by unchanged by this middleware.
|
||||
///
|
||||
/// # Examples
|
||||
/// ## Handler Response
|
||||
/// Header
|
||||
/// ```
|
||||
/// use actix_web::http::{header, StatusCode};
|
||||
/// use actix_web::middleware::{ErrorHandlerResponse, ErrorHandlers};
|
||||
|
@ -67,6 +69,28 @@ type DefaultHandler<B> = Option<Rc<ErrorHandler<B>>>;
|
|||
/// .wrap(ErrorHandlers::new().handler(StatusCode::INTERNAL_SERVER_ERROR, add_error_header))
|
||||
/// .service(web::resource("/").route(web::get().to(HttpResponse::InternalServerError)));
|
||||
/// ```
|
||||
///
|
||||
/// Body Content
|
||||
/// ```
|
||||
/// use actix_web::http::{header, StatusCode};
|
||||
/// use actix_web::middleware::{ErrorHandlerResponse, ErrorHandlers};
|
||||
/// use actix_web::{dev, web, App, HttpResponse, Result};
|
||||
/// fn add_error_body<B>(res: dev::ServiceResponse<B>) -> Result<ErrorHandlerResponse<B>> {
|
||||
/// // Get the error message and status code
|
||||
/// let error_message = "An error occurred";
|
||||
/// // Destructures ServiceResponse into request and response components
|
||||
/// let (req, res) = res.into_parts();
|
||||
/// // Create a new response with the modified body
|
||||
/// let res = res.set_body(error_message).map_into_boxed_body();
|
||||
/// // Create a new ServiceResponse with the modified response
|
||||
/// let res = dev::ServiceResponse::new(req, res).map_into_right_body();
|
||||
/// Ok(ErrorHandlerResponse::Response(res))
|
||||
///}
|
||||
///
|
||||
/// let app = App::new()
|
||||
/// .wrap(ErrorHandlers::new().handler(StatusCode::INTERNAL_SERVER_ERROR, add_error_body))
|
||||
/// .service(web::resource("/").route(web::get().to(HttpResponse::InternalServerError)));
|
||||
/// ```
|
||||
/// ## Registering default handler
|
||||
/// ```
|
||||
/// # use actix_web::http::{header, StatusCode};
|
||||
|
|
Loading…
Reference in a new issue