{"id":1192,"hash":"fe4e1812e761aff138acff9213d07a30158ff84bc4e8f5274b68b1dcf3150df5","pattern":"How to return the error description in a invalid JSON request body to the client in Rust?","full_message":"In Python, I can use marshmallow or Pydantic to validate user input just by defining a schema (much like a Rust struct). Then using that schema, Marshmallow loads the user input and returns the error it finds. Such as:\n\nI can have custom error handle in actix-web by implementing ResponseError.\n\nBut, how can I return the description (position/field of invalid value) in a bad request body to the client?","ecosystem":"cargo","package_name":"actix-web","package_version":null,"solution":"I am always looking for answers.\n\nUnfortunately, some web frameworks including actix handle JSON error before we can do any validation. I keep searching for using various keywords. One of them is \"actix-web validate JSON\" which leads me to many validator crates. But I get insight from this blog saying that:\n\nExtractors are helpers that implement the FromRequest trait. In other words, they construct any object from a request and perform validation along the way. A handful of useful extractors are shipped with Actix web, such as JSON which uses serde_json to deserialize a JSON request body\n\nSo, I search for \"actix Extractor\" and bring me Extractor Doc and custom handling of extractor errors\n\nSo this snippet is taken from this boilerplate solves my current problem.\n\n   App::new()\n       .configure(health::init)\n       .configure(students::init)\n+      .app_data(web::JsonConfig::default().error_handler(|err, _req| {\n+          error::InternalError::from_response(\n+              \"\",\n+              HttpResponse::BadRequest()\n+                  .content_type(\"application/json\")\n+                  .body(format!(r#\"{{\"error\":\"{}\"}}\"#, err)),\n+          )\n+          .into()\n+      }))","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/64291039/how-to-return-the-error-description-in-a-invalid-json-request-body-to-the-client","votes":8,"created_at":"2026-04-19T04:52:34.054021+00:00","updated_at":"2026-04-19T04:52:34.054021+00:00"}