mirror of
https://github.com/actix/examples
synced 2025-06-27 01:27:43 +02:00
Restructure folders (#411)
This commit is contained in:
committed by
GitHub
parent
9db98162b2
commit
c3407627d0
44
json/json_decode_error/src/main.rs
Normal file
44
json/json_decode_error/src/main.rs
Normal file
@ -0,0 +1,44 @@
|
||||
use actix_web::{
|
||||
error, post, web, App, 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_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new().service(greet).app_data(
|
||||
web::JsonConfig::default()
|
||||
// register error_handler for JSON extractors.
|
||||
.error_handler(json_error_handler),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.await
|
||||
}
|
Reference in New Issue
Block a user