Refactor errors internals (#556)
h2::Error now knows whether protocol errors happened because the user sent them, because it was received from the remote peer, or because the library itself emitted an error because it detected a protocol violation. It also keeps track of whether it came from a RST_STREAM or GO_AWAY frame, and in the case of the latter, it includes the additional debug data if any. Fixes #530
This commit is contained in:
68
src/error.rs
68
src/error.rs
@@ -1,6 +1,8 @@
|
||||
use crate::codec::{SendError, UserError};
|
||||
use crate::proto;
|
||||
use crate::frame::StreamId;
|
||||
use crate::proto::{self, Initiator};
|
||||
|
||||
use bytes::Bytes;
|
||||
use std::{error, fmt, io};
|
||||
|
||||
pub use crate::frame::Reason;
|
||||
@@ -22,11 +24,14 @@ pub struct Error {
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Kind {
|
||||
/// An error caused by an action taken by the remote peer.
|
||||
///
|
||||
/// This is either an error received by the peer or caused by an invalid
|
||||
/// action taken by the peer (i.e. a protocol error).
|
||||
Proto(Reason),
|
||||
/// A RST_STREAM frame was received or sent.
|
||||
Reset(StreamId, Reason, Initiator),
|
||||
|
||||
/// A GO_AWAY frame was received or sent.
|
||||
GoAway(Bytes, Reason, Initiator),
|
||||
|
||||
/// The user created an error from a bare Reason.
|
||||
Reason(Reason),
|
||||
|
||||
/// An error resulting from an invalid action taken by the user of this
|
||||
/// library.
|
||||
@@ -45,7 +50,7 @@ impl Error {
|
||||
/// action taken by the peer (i.e. a protocol error).
|
||||
pub fn reason(&self) -> Option<Reason> {
|
||||
match self.kind {
|
||||
Kind::Proto(reason) => Some(reason),
|
||||
Kind::Reset(_, reason, _) | Kind::GoAway(_, reason, _) => Some(reason),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -87,8 +92,13 @@ impl From<proto::Error> for Error {
|
||||
|
||||
Error {
|
||||
kind: match src {
|
||||
Proto(reason) => Kind::Proto(reason),
|
||||
Io(e) => Kind::Io(e),
|
||||
Reset(stream_id, reason, initiator) => Kind::Reset(stream_id, reason, initiator),
|
||||
GoAway(debug_data, reason, initiator) => {
|
||||
Kind::GoAway(debug_data, reason, initiator)
|
||||
}
|
||||
Io(kind, inner) => {
|
||||
Kind::Io(inner.map_or_else(|| kind.into(), |inner| io::Error::new(kind, inner)))
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -97,7 +107,7 @@ impl From<proto::Error> for Error {
|
||||
impl From<Reason> for Error {
|
||||
fn from(src: Reason) -> Error {
|
||||
Error {
|
||||
kind: Kind::Proto(src),
|
||||
kind: Kind::Reason(src),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -106,8 +116,7 @@ impl From<SendError> for Error {
|
||||
fn from(src: SendError) -> Error {
|
||||
match src {
|
||||
SendError::User(e) => e.into(),
|
||||
SendError::Connection(reason) => reason.into(),
|
||||
SendError::Io(e) => Error::from_io(e),
|
||||
SendError::Connection(e) => e.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -122,13 +131,38 @@ impl From<UserError> for Error {
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
use self::Kind::*;
|
||||
let debug_data = match self.kind {
|
||||
Kind::Reset(_, reason, Initiator::User) => {
|
||||
return write!(fmt, "stream error sent by user: {}", reason)
|
||||
}
|
||||
Kind::Reset(_, reason, Initiator::Library) => {
|
||||
return write!(fmt, "stream error detected: {}", reason)
|
||||
}
|
||||
Kind::Reset(_, reason, Initiator::Remote) => {
|
||||
return write!(fmt, "stream error received: {}", reason)
|
||||
}
|
||||
Kind::GoAway(ref debug_data, reason, Initiator::User) => {
|
||||
write!(fmt, "connection error sent by user: {}", reason)?;
|
||||
debug_data
|
||||
}
|
||||
Kind::GoAway(ref debug_data, reason, Initiator::Library) => {
|
||||
write!(fmt, "connection error detected: {}", reason)?;
|
||||
debug_data
|
||||
}
|
||||
Kind::GoAway(ref debug_data, reason, Initiator::Remote) => {
|
||||
write!(fmt, "connection error received: {}", reason)?;
|
||||
debug_data
|
||||
}
|
||||
Kind::Reason(reason) => return write!(fmt, "protocol error: {}", reason),
|
||||
Kind::User(ref e) => return write!(fmt, "user error: {}", e),
|
||||
Kind::Io(ref e) => return e.fmt(fmt),
|
||||
};
|
||||
|
||||
match self.kind {
|
||||
Proto(ref reason) => write!(fmt, "protocol error: {}", reason),
|
||||
User(ref e) => write!(fmt, "user error: {}", e),
|
||||
Io(ref e) => fmt::Display::fmt(e, fmt),
|
||||
if !debug_data.is_empty() {
|
||||
write!(fmt, " ({:?})", debug_data)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user