1
0
mirror of https://github.com/actix/examples synced 2025-02-02 17:39:05 +01:00
Stig Johan Berggren 8f3ce39d05
New example: Return JSON decode errors to a client (#268)
* Add JSON decode error example

* cargo fmt

* Use more appropriate response codes

* curl -i similar for all examples

* Add one more example of 422 response

* use to_string instead of empty format!
2020-03-16 03:21:13 +09:00

45 lines
1.2 KiB
Rust

use actix_web::{
error, post, web, App, FromRequest, HttpRequest, HttpResponse, HttpServer, Responder,
};
use serde::Deserialize;
#[derive(Deserialize)]
struct Info {
name: String,
}
#[post("/")]
async fn greet(name: web::Json<Info>) -> impl Responder {
HttpResponse::Ok().body(format!("Hello {}!", name.name))
}
fn json_error_handler(err: error::JsonPayloadError, _req: &HttpRequest) -> error::Error {
use actix_web::error::JsonPayloadError;
let detail = err.to_string();
let resp = match &err {
JsonPayloadError::ContentType => {
HttpResponse::UnsupportedMediaType().body(detail)
}
JsonPayloadError::Deserialize(json_err) if json_err.is_data() => {
HttpResponse::UnprocessableEntity().body(detail)
}
_ => HttpResponse::BadRequest().body(detail),
};
error::InternalError::from_response(err, resp).into()
}
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.service(greet)
.app_data(web::Json::<Info>::configure(|cfg| {
cfg.error_handler(json_error_handler)
}))
})
.bind("127.0.0.1:8088")?
.run()
.await
}