Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Same message duplicated between source() and Display of serde_yaml::Error #359

Closed
dtolnay opened this issue Mar 5, 2023 · 0 comments · Fixed by #360
Closed

Same message duplicated between source() and Display of serde_yaml::Error #359

dtolnay opened this issue Mar 5, 2023 · 0 comments · Fixed by #360

Comments

@dtolnay
Copy link
Owner

dtolnay commented Mar 5, 2023

// [dependencies]
// anyhow = "1"
// serde = "1"
// serde_yaml = "0.9"

use serde::de::Deserialize;
use std::error::Error;
use std::fmt::{self, Display};
use std::io::{self, Read};

struct Reader;

#[derive(Debug)]
struct MyError;

impl Error for MyError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        Some(&std::sync::mpsc::RecvTimeoutError::Timeout)
    }
}

impl Display for MyError {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("bad read")
    }
}

impl Read for Reader {
    fn read(&mut self, _: &mut [u8]) -> io::Result<usize> {
        Err(io::Error::new(io::ErrorKind::TimedOut, MyError))
    }
}

fn main() -> anyhow::Result<()> {
    let reader = Reader;
    let de = serde_yaml::Deserializer::from_reader(reader);
    let _ = u8::deserialize(de)?;
    Ok(())
}
Error: bad read

Caused by:
    0: bad read
    1: timed out waiting on channel

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

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

Successfully merging a pull request may close this issue.

1 participant