Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error messages customization #1207

Open
avandecreme opened this issue Oct 28, 2024 · 0 comments
Open

Error messages customization #1207

avandecreme opened this issue Oct 28, 2024 · 0 comments

Comments

@avandecreme
Copy link

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:

  1. 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.
  2. 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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant