1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-02 21:39:26 +00:00

📝 Improve the code example for JsonConfig (#1418)

* 📝 Improve the code example for JsonConfig

* Remove a redundant comment
This commit is contained in:
Stig Johan Berggren 2020-03-17 00:23:54 +01:00 committed by GitHub
parent c67e4c1fe8
commit 0d958fabd7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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))
/// );