diff --git a/src/codec/error.rs b/src/codec/error.rs index 0acb913e..c3dd9a77 100644 --- a/src/codec/error.rs +++ b/src/codec/error.rs @@ -1,6 +1,6 @@ use crate::proto::Error; -use std::{error, fmt, io}; +use std::{fmt, io}; /// Errors caused by sending a message #[derive(Debug)] @@ -53,8 +53,6 @@ pub enum UserError { // ===== impl SendError ===== -impl error::Error for SendError {} - impl fmt::Display for SendError { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { match *self { @@ -78,8 +76,6 @@ impl From for SendError { // ===== impl UserError ===== -impl error::Error for UserError {} - impl fmt::Display for UserError { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { use self::UserError::*; diff --git a/src/error.rs b/src/error.rs index 96a471bc..69831f3c 100644 --- a/src/error.rs +++ b/src/error.rs @@ -196,10 +196,20 @@ impl fmt::Display for Error { } } -impl error::Error for Error {} +impl error::Error for Error { + fn source(&self) -> Option<&(dyn error::Error + 'static)> { + match self.kind { + Kind::Io(ref e) => Some(e), + _ => None, + } + } +} #[cfg(test)] mod tests { + use std::error::Error as _; + use std::io; + use super::Error; use crate::Reason; @@ -208,4 +218,14 @@ mod tests { let err = Error::from(Reason::HTTP_1_1_REQUIRED); assert_eq!(err.reason(), Some(Reason::HTTP_1_1_REQUIRED)); } + + #[test] + fn io_error_source() { + let err = Error::from_io(io::Error::new(io::ErrorKind::BrokenPipe, "hi")); + let source = err.source().expect("io error should have source"); + let io_err = source + .downcast_ref::() + .expect("should be io error"); + assert_eq!(io_err.kind(), io::ErrorKind::BrokenPipe); + } }