mirror of
https://github.com/actix/actix-web.git
synced 2025-01-02 21:38:46 +00:00
📝 Improve the code example for JsonConfig (#1418)
* 📝 Improve the code example for JsonConfig
* Remove a redundant comment
This commit is contained in:
parent
c67e4c1fe8
commit
0d958fabd7
1 changed files with 17 additions and 5 deletions
|
@ -207,8 +207,10 @@ where
|
|||
|
||||
/// Json extractor configuration
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// use actix_web::{error, web, App, FromRequest, HttpResponse};
|
||||
/// use actix_web::{error, web, App, FromRequest, HttpRequest, HttpResponse};
|
||||
/// use serde_derive::Deserialize;
|
||||
///
|
||||
/// #[derive(Deserialize)]
|
||||
|
@ -221,6 +223,19 @@ where
|
|||
/// format!("Welcome {}!", info.username)
|
||||
/// }
|
||||
///
|
||||
/// /// Return either a 400 or 415, and include the error message from serde
|
||||
/// /// in the response body
|
||||
/// fn json_error_handler(err: error::JsonPayloadError, _req: &HttpRequest) -> error::Error {
|
||||
/// let detail = err.to_string();
|
||||
/// let response = match &err {
|
||||
/// error::JsonPayloadError::ContentType => {
|
||||
/// HttpResponse::UnsupportedMediaType().content_type("text/plain").body(detail)
|
||||
/// }
|
||||
/// _ => HttpResponse::BadRequest().content_type("text/plain").body(detail),
|
||||
/// };
|
||||
/// error::InternalError::from_response(err, response).into()
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = App::new().service(
|
||||
/// web::resource("/index.html")
|
||||
|
@ -231,10 +246,7 @@ where
|
|||
/// .content_type(|mime| { // <- accept text/plain content type
|
||||
/// mime.type_() == mime::TEXT && mime.subtype() == mime::PLAIN
|
||||
/// })
|
||||
/// .error_handler(|err, req| { // <- create custom error response
|
||||
/// error::InternalError::from_response(
|
||||
/// err, HttpResponse::Conflict().finish()).into()
|
||||
/// })
|
||||
/// .error_handler(json_error_handler) // Use our custom error response
|
||||
/// }))
|
||||
/// .route(web::post().to(index))
|
||||
/// );
|
||||
|
|
Loading…
Reference in a new issue