dtolnay / path-to-error

Find out path at which a deserialization error occurred
Apache License 2.0
320 stars 12 forks source link

Fix message duplication between error Display and source() #20

Closed dtolnay closed 1 year ago

dtolnay commented 1 year ago

Example:

use serde::Deserialize;

#[derive(Deserialize)]
pub struct Struct {
    pub x: i32,
}

fn main() -> anyhow::Result<()> {
    let j = "{\"x\":true}";
    let mut de = serde_json::Deserializer::from_str(j);
    let _: Struct = serde_path_to_error::deserialize(&mut de)?;
    Ok(())
}

Previously this would print:

Error: x: invalid type: boolean `true`, expected i32 at line 1 column 9

Caused by:
    invalid type: boolean `true`, expected i32 at line 1 column 9

In general, std::error::Error impls are not supposed to duplicate the same content between their Display and source() implementation.