Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Error::source for h2::Error. #462

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/codec/error.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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 {
Expand All @@ -78,8 +76,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
22 changes: 21 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
goffrie marked this conversation as resolved.
Show resolved Hide resolved
_ => None,
}
}
}

#[cfg(test)]
mod tests {
use std::error::Error as _;
use std::io;

use super::Error;
use crate::Reason;

Expand All @@ -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::<io::Error>()
.expect("should be io error");
assert_eq!(io_err.kind(), io::ErrorKind::BrokenPipe);
}
}