Error messages customization
Opened this issue · 0 comments
avandecreme commented
The following code:
use serde; // 1.0.124
use serde_json; // 1.0.64
use serde::Deserialize;
#[derive(Debug, Deserialize)]
pub struct Foo {
bar: u32,
baz: String,
}
fn main() {
serde_json::from_str::<Foo>(r#"{ "bar": "bar", "baz": "baz" }"#)
.inspect(|x| println!("{x:?}"))
.inspect_err(|err| println!("{err}"));
}
prints:
invalid type: string "bar", expected u32 at line 1 column 14
I would like to customize the error message because it doesn't quite match what I would expect a web server to return:
- The line and column number is often not relevant, the payloads are often sent on one line and the column number would be very high. I would rather have the path returned by serde_path_to_error instead.
- The u32 type is really rust specific, saying integer or positive integer would be better.
So here is the ideal error message for me:
"bar": invalid type, got string "bar", expected integer
I looked at building the message myself with the help of serde_path_to_error
, unfortunately the Error type of this crate has most interesting things as private. There is for example no way to access the ErrorCode
.
Would you consider making it possible to customize the error messages one way or another?