If the payload is invalid, the server throws back to the user a message e.g. Json deserialize error: invalid type: Z, expected a string at line X column Y, this is extremely undeseriable in production. I know there is JsonConfig but this works only per each T so it's pointless and exhausting to initialize it for each API endpoint, there should be a way to make this error handling global or anything that works at the App level. Is there any workaround or a solution for this as of the latest sable release? Thank you.
Seems a bit like this is really an issue for the maintainers of serde_json to improve the error it returns on a parse failure.
Even if I were to customize per T, I'm not quite sure how I'd reason about giving useful more feedback, at least in a generic or procedural way.
@onelson thank you for the response, I am not concerned about the exact error produced by serde, I am concerned actually about not throwing off that kind of revealing messages like (Json deserialize error: invalid type: Z, expected a string at line X column Y) about the system, I just want to override whatever error produced by the deserializer and replace it with some custom json message compatible with my API design. Something like "{"error": "deserialization error"} that works at the App level
@gh67uyyghj You can registeir your original error handler. Check below.
https://docs.rs/actix-web/1.0.9/actix_web/web/struct.JsonConfig.html
@alu thank you, I know of this solution but as I said in the post this works per type, I must repeat this for each handler that accept json body requests, which is pretty much the entire API. I was asking if any generic solution actually currently exists at the App or Service level.
@gh67uyyghj
You can register custom error handler as default such as below.
App::new()
.data(web::JsonConfig::default().error_handler(|_, _| ...)))
@alu wow thank you! this totally resolves my issue. Thanks.
@alu I'm using actix-web 2.0.0, and it seems that
App::new()
.data(web::JsonConfig::default().error_handler(|_, _| ...)))
doesn't work, when serde failed to parse the JSON, the custom error handler I write won't be called. Anyone has any idea about this?
@alu I'm using actix-web 2.0.0, and it seems that
App::new() .data(web::JsonConfig::default().error_handler(|_, _| ...)))doesn't work, when serde failed to parse the JSON, the custom error handler I write won't be called. Anyone has any idea about this?
+1
@alu I'm using actix-web 2.0.0, and it seems that
App::new() .data(web::JsonConfig::default().error_handler(|_, _| ...)))doesn't work, when serde failed to parse the JSON, the custom error handler I write won't be called. Anyone has any idea about this?
Still works with .app_data()
Most helpful comment
Still works with .app_data()