From 5e64fc4ff668179642a17f919b314b392cd2c93a Mon Sep 17 00:00:00 2001 From: Geoffry Song Date: Wed, 29 Apr 2020 23:56:53 -0700 Subject: [PATCH 1/2] Implement `Error::source` for `h2::Error`. This makes it easier to detect an IO error at the bottom of an error cause chain (e.g. behind a `hyper::Error`). Also, don't implement `std::error::Error` for the private error types, to avoid confusion. --- src/codec/error.rs | 8 +------- src/error.rs | 9 ++++++++- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/codec/error.rs b/src/codec/error.rs index 2c6b2961d..87ab82bf9 100644 --- a/src/codec/error.rs +++ b/src/codec/error.rs @@ -1,6 +1,6 @@ use crate::frame::{Reason, StreamId}; -use std::{error, fmt, io}; +use std::{fmt, io}; /// Errors that are received #[derive(Debug)] @@ -73,8 +73,6 @@ impl From for RecvError { } } -impl error::Error for RecvError {} - impl fmt::Display for RecvError { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { use self::RecvError::*; @@ -89,8 +87,6 @@ impl fmt::Display for RecvError { // ===== impl SendError ===== -impl error::Error for SendError {} - impl fmt::Display for SendError { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { use self::SendError::*; @@ -117,8 +113,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 372bac2ee..71f119057 100644 --- a/src/error.rs +++ b/src/error.rs @@ -132,4 +132,11 @@ 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, + } + } +} From e4d3b6167a3cd352c9e98397b5400c435af87005 Mon Sep 17 00:00:00 2001 From: Geoffry Song Date: Mon, 29 Apr 2024 10:16:56 -0700 Subject: [PATCH 2/2] fmt --- src/error.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/error.rs b/src/error.rs index 02d91cbec..69831f3c5 100644 --- a/src/error.rs +++ b/src/error.rs @@ -223,7 +223,9 @@ mod tests { 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"); + let io_err = source + .downcast_ref::() + .expect("should be io error"); assert_eq!(io_err.kind(), io::ErrorKind::BrokenPipe); } }