Skip to content

Commit

Permalink
Implement Error::source for h2::Error.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
goffrie committed Apr 30, 2020
1 parent cfc7584 commit 5e64fc4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
8 changes: 1 addition & 7 deletions src/codec/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::frame::{Reason, StreamId};

use std::{error, fmt, io};
use std::{fmt, io};

/// Errors that are received
#[derive(Debug)]
Expand Down Expand Up @@ -73,8 +73,6 @@ impl From<io::Error> for RecvError {
}
}

impl error::Error for RecvError {}

impl fmt::Display for RecvError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
use self::RecvError::*;
Expand All @@ -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::*;
Expand All @@ -117,8 +113,6 @@ impl From<UserError> 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::*;
Expand Down
9 changes: 8 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}

0 comments on commit 5e64fc4

Please sign in to comment.