diff --git a/src/client/dispatch.rs b/src/client/dispatch.rs index 484cb04f4b..f4b84a014f 100644 --- a/src/client/dispatch.rs +++ b/src/client/dispatch.rs @@ -317,7 +317,7 @@ mod tests { .expect("fulfilled") .expect_err("promise should error"); match (err.0.kind(), err.1) { - (&crate::error::Kind::Canceled, Some(_)) => (), + (&crate::error::Kind::Canceled(_), Some(_)) => (), e => panic!("expected Error::Cancel(_), found {:?}", e), } } diff --git a/src/client/service.rs b/src/client/service.rs index 4013c5e54e..176555a4e5 100644 --- a/src/client/service.rs +++ b/src/client/service.rs @@ -55,7 +55,7 @@ where fn poll_ready(&mut self, cx: &mut task::Context<'_>) -> Poll> { self.inner .poll_ready(cx) - .map_err(|e| crate::Error::new(crate::error::Kind::Connect).with(e.into())) + .map_err(|e| crate::Error::new_connect(e)) } fn call(&mut self, req: T) -> Self::Future { @@ -76,7 +76,7 @@ where Err(e) => Err(e), }, Err(e) => { - let err = crate::Error::new(crate::error::Kind::Connect).with(e.into()); + let err = crate::Error::new_connect(e); Err(err) } } diff --git a/src/error.rs b/src/error.rs index 663156e0a9..45af82fec8 100644 --- a/src/error.rs +++ b/src/error.rs @@ -2,6 +2,8 @@ use std::error::Error as StdError; use std::fmt; +use self::sealed::Sealed; + /// Result type often returned from methods that can have hyper `Error`s. pub type Result = std::result::Result; @@ -17,109 +19,130 @@ struct ErrorImpl { cause: Option, } +/// Represents the kind of the error. +/// +/// This enum is non-exhaustive. #[derive(Debug, PartialEq)] -pub(super) enum Kind { +#[non_exhaustive] +pub enum Kind { + /// Error occured while parsing. Parse(Parse), + /// Error occured while executing user code. User(User), /// A message reached EOF, but is not complete. - IncompleteMessage, + IncompleteMessage(Sealed), /// A connection received a message (or bytes) when not waiting for one. #[cfg(feature = "http1")] - UnexpectedMessage, + UnexpectedMessage(Sealed), /// A pending item was dropped before ever being processed. - Canceled, + Canceled(Sealed), /// Indicates a channel (client or body sender) is closed. - ChannelClosed, + ChannelClosed(Sealed), /// An `io::Error` that occurred while trying to read or write to a network stream. #[cfg(any(feature = "http1", feature = "http2"))] - Io, + Io(Sealed), /// Error occurred while connecting. - Connect, + Connect(Sealed), /// Error creating a TcpListener. #[cfg(all( any(feature = "http1", feature = "http2"), feature = "tcp", feature = "server" ))] - Listen, + Listen(Sealed), /// Error accepting on an Incoming stream. #[cfg(any(feature = "http1", feature = "http2"))] #[cfg(feature = "server")] - Accept, + Accept(Sealed), /// Error while reading a body from connection. #[cfg(any(feature = "http1", feature = "http2", feature = "stream"))] - Body, + Body(Sealed), /// Error while writing a body to connection. #[cfg(any(feature = "http1", feature = "http2"))] - BodyWrite, + BodyWrite(Sealed), /// The body write was aborted. - BodyWriteAborted, + BodyWriteAborted(Sealed), /// Error calling AsyncWrite::shutdown() #[cfg(feature = "http1")] - Shutdown, + Shutdown(Sealed), /// A general error from h2. #[cfg(feature = "http2")] - Http2, + Http2(Sealed), } +/// Represents the kind of the parse error. +/// +/// This enum is non-exhaustive. #[derive(Debug, PartialEq)] -pub(super) enum Parse { +#[non_exhaustive] +pub enum Parse { + /// Invalid HTTP method parsed. Method, + /// Invalid HTTP version parsed. Version, + /// Invalid HTTP version parsed (found HTTP2 preface). #[cfg(feature = "http1")] VersionH2, + /// Invalid URI. Uri, + /// Invalid HTTP header parsed. Header, + /// Message head is too large. TooLarge, + /// Invalid HTTP status-code parsed. Status, } +/// Represents the kind of the user error. +/// +/// This enum is non-exhaustive. #[derive(Debug, PartialEq)] -pub(super) enum User { +#[non_exhaustive] +pub enum User { /// Error calling user's HttpBody::poll_data(). #[cfg(any(feature = "http1", feature = "http2"))] - Body, + Body(Sealed), /// Error calling user's MakeService. #[cfg(any(feature = "http1", feature = "http2"))] #[cfg(feature = "server")] - MakeService, + MakeService(Sealed), /// Error from future of user's Service. #[cfg(any(feature = "http1", feature = "http2"))] - Service, + Service(Sealed), /// User tried to send a certain header in an unexpected context. /// /// For example, sending both `content-length` and `transfer-encoding`. #[cfg(feature = "http1")] #[cfg(feature = "server")] - UnexpectedHeader, + UnexpectedHeader(Sealed), /// User tried to create a Request with bad version. #[cfg(any(feature = "http1", feature = "http2"))] #[cfg(feature = "client")] - UnsupportedVersion, + UnsupportedVersion(Sealed), /// User tried to create a CONNECT Request with the Client. #[cfg(any(feature = "http1", feature = "http2"))] #[cfg(feature = "client")] - UnsupportedRequestMethod, + UnsupportedRequestMethod(Sealed), /// User tried to respond with a 1xx (not 101) response code. #[cfg(feature = "http1")] #[cfg(feature = "server")] - UnsupportedStatusCode, + UnsupportedStatusCode(Sealed), /// User tried to send a Request with Client with non-absolute URI. #[cfg(any(feature = "http1", feature = "http2"))] #[cfg(feature = "client")] - AbsoluteUriRequired, + AbsoluteUriRequired(Sealed), /// User tried polling for an upgrade that doesn't exist. - NoUpgrade, + NoUpgrade(Sealed), /// User polled for an upgrade, but low-level API is not using upgrades. #[cfg(feature = "http1")] - ManualUpgrade, + ManualUpgrade(Sealed), /// User aborted in an FFI callback. #[cfg(feature = "ffi")] - AbortedByCallback, + AbortedByCallback(Sealed), } // Sentinel type to indicate the error was caused by a timeout. @@ -139,27 +162,27 @@ impl Error { /// Returns true if this was about a `Request` that was canceled. pub fn is_canceled(&self) -> bool { - self.inner.kind == Kind::Canceled + matches!(self.inner.kind, Kind::Canceled(_)) } /// Returns true if a sender's channel is closed. pub fn is_closed(&self) -> bool { - self.inner.kind == Kind::ChannelClosed + matches!(self.inner.kind, Kind::ChannelClosed(_)) } /// Returns true if this was an error from `Connect`. pub fn is_connect(&self) -> bool { - self.inner.kind == Kind::Connect + matches!(self.inner.kind, Kind::Connect(_)) } /// Returns true if the connection closed before a message could complete. pub fn is_incomplete_message(&self) -> bool { - self.inner.kind == Kind::IncompleteMessage + matches!(self.inner.kind, Kind::IncompleteMessage(_)) } /// Returns true if the body write was aborted. pub fn is_body_write_aborted(&self) -> bool { - self.inner.kind == Kind::BodyWriteAborted + matches!(self.inner.kind, Kind::BodyWriteAborted(_)) } /// Returns true if the error was caused by a timeout. @@ -211,12 +234,12 @@ impl Error { } pub(super) fn new_canceled() -> Error { - Error::new(Kind::Canceled) + Error::new(Kind::Canceled(Sealed)) } #[cfg(feature = "http1")] pub(super) fn new_incomplete() -> Error { - Error::new(Kind::IncompleteMessage) + Error::new(Kind::IncompleteMessage(Sealed)) } #[cfg(feature = "http1")] @@ -231,48 +254,48 @@ impl Error { #[cfg(feature = "http1")] pub(super) fn new_unexpected_message() -> Error { - Error::new(Kind::UnexpectedMessage) + Error::new(Kind::UnexpectedMessage(Sealed)) } #[cfg(any(feature = "http1", feature = "http2"))] pub(super) fn new_io(cause: std::io::Error) -> Error { - Error::new(Kind::Io).with(cause) + Error::new(Kind::Io(Sealed)).with(cause) } #[cfg(all(any(feature = "http1", feature = "http2"), feature = "tcp"))] #[cfg(feature = "server")] pub(super) fn new_listen>(cause: E) -> Error { - Error::new(Kind::Listen).with(cause) + Error::new(Kind::Listen(Sealed)).with(cause) } #[cfg(any(feature = "http1", feature = "http2"))] #[cfg(feature = "server")] pub(super) fn new_accept>(cause: E) -> Error { - Error::new(Kind::Accept).with(cause) + Error::new(Kind::Accept(Sealed)).with(cause) } #[cfg(any(feature = "http1", feature = "http2"))] #[cfg(feature = "client")] pub(super) fn new_connect>(cause: E) -> Error { - Error::new(Kind::Connect).with(cause) + Error::new(Kind::Connect(Sealed)).with(cause) } pub(super) fn new_closed() -> Error { - Error::new(Kind::ChannelClosed) + Error::new(Kind::ChannelClosed(Sealed)) } #[cfg(any(feature = "http1", feature = "http2", feature = "stream"))] pub(super) fn new_body>(cause: E) -> Error { - Error::new(Kind::Body).with(cause) + Error::new(Kind::Body(Sealed)).with(cause) } #[cfg(any(feature = "http1", feature = "http2"))] pub(super) fn new_body_write>(cause: E) -> Error { - Error::new(Kind::BodyWrite).with(cause) + Error::new(Kind::BodyWrite(Sealed)).with(cause) } pub(super) fn new_body_write_aborted() -> Error { - Error::new(Kind::BodyWriteAborted) + Error::new(Kind::BodyWriteAborted(Sealed)) } fn new_user(user: User) -> Error { @@ -282,61 +305,61 @@ impl Error { #[cfg(feature = "http1")] #[cfg(feature = "server")] pub(super) fn new_user_header() -> Error { - Error::new_user(User::UnexpectedHeader) + Error::new_user(User::UnexpectedHeader(Sealed)) } #[cfg(any(feature = "http1", feature = "http2"))] #[cfg(feature = "client")] pub(super) fn new_user_unsupported_version() -> Error { - Error::new_user(User::UnsupportedVersion) + Error::new_user(User::UnsupportedVersion(Sealed)) } #[cfg(any(feature = "http1", feature = "http2"))] #[cfg(feature = "client")] pub(super) fn new_user_unsupported_request_method() -> Error { - Error::new_user(User::UnsupportedRequestMethod) + Error::new_user(User::UnsupportedRequestMethod(Sealed)) } #[cfg(feature = "http1")] #[cfg(feature = "server")] pub(super) fn new_user_unsupported_status_code() -> Error { - Error::new_user(User::UnsupportedStatusCode) + Error::new_user(User::UnsupportedStatusCode(Sealed)) } #[cfg(any(feature = "http1", feature = "http2"))] #[cfg(feature = "client")] pub(super) fn new_user_absolute_uri_required() -> Error { - Error::new_user(User::AbsoluteUriRequired) + Error::new_user(User::AbsoluteUriRequired(Sealed)) } pub(super) fn new_user_no_upgrade() -> Error { - Error::new_user(User::NoUpgrade) + Error::new_user(User::NoUpgrade(Sealed)) } #[cfg(feature = "http1")] pub(super) fn new_user_manual_upgrade() -> Error { - Error::new_user(User::ManualUpgrade) + Error::new_user(User::ManualUpgrade(Sealed)) } #[cfg(any(feature = "http1", feature = "http2"))] #[cfg(feature = "server")] pub(super) fn new_user_make_service>(cause: E) -> Error { - Error::new_user(User::MakeService).with(cause) + Error::new_user(User::MakeService(Sealed)).with(cause) } #[cfg(any(feature = "http1", feature = "http2"))] pub(super) fn new_user_service>(cause: E) -> Error { - Error::new_user(User::Service).with(cause) + Error::new_user(User::Service(Sealed)).with(cause) } #[cfg(any(feature = "http1", feature = "http2"))] pub(super) fn new_user_body>(cause: E) -> Error { - Error::new_user(User::Body).with(cause) + Error::new_user(User::Body(Sealed)).with(cause) } #[cfg(feature = "http1")] pub(super) fn new_shutdown(cause: std::io::Error) -> Error { - Error::new(Kind::Shutdown).with(cause) + Error::new(Kind::Shutdown(Sealed)).with(cause) } #[cfg(feature = "ffi")] @@ -349,10 +372,15 @@ impl Error { if cause.is_io() { Error::new_io(cause.into_io().expect("h2::Error::is_io")) } else { - Error::new(Kind::Http2).with(cause) + Error::new_fake_h2(cause) } } + #[cfg(feature = "http2")] + pub(super) fn new_fake_h2>(cause: E) -> Error { + Error::new(Kind::Http2(Sealed)).with(cause) + } + fn description(&self) -> &str { match self.inner.kind { Kind::Parse(Parse::Method) => "invalid HTTP method parsed", @@ -363,57 +391,57 @@ impl Error { Kind::Parse(Parse::Header) => "invalid HTTP header parsed", Kind::Parse(Parse::TooLarge) => "message head is too large", Kind::Parse(Parse::Status) => "invalid HTTP status-code parsed", - Kind::IncompleteMessage => "connection closed before message completed", + Kind::IncompleteMessage(_) => "connection closed before message completed", #[cfg(feature = "http1")] - Kind::UnexpectedMessage => "received unexpected message from connection", - Kind::ChannelClosed => "channel closed", - Kind::Connect => "error trying to connect", - Kind::Canceled => "operation was canceled", + Kind::UnexpectedMessage(_) => "received unexpected message from connection", + Kind::ChannelClosed(_) => "channel closed", + Kind::Connect(_) => "error trying to connect", + Kind::Canceled(_) => "operation was canceled", #[cfg(all(any(feature = "http1", feature = "http2"), feature = "tcp"))] #[cfg(feature = "server")] - Kind::Listen => "error creating server listener", + Kind::Listen(_) => "error creating server listener", #[cfg(any(feature = "http1", feature = "http2"))] #[cfg(feature = "server")] - Kind::Accept => "error accepting connection", + Kind::Accept(_) => "error accepting connection", #[cfg(any(feature = "http1", feature = "http2", feature = "stream"))] - Kind::Body => "error reading a body from connection", + Kind::Body(_) => "error reading a body from connection", #[cfg(any(feature = "http1", feature = "http2"))] - Kind::BodyWrite => "error writing a body to connection", - Kind::BodyWriteAborted => "body write aborted", + Kind::BodyWrite(_) => "error writing a body to connection", + Kind::BodyWriteAborted(_) => "body write aborted", #[cfg(feature = "http1")] - Kind::Shutdown => "error shutting down connection", + Kind::Shutdown(_) => "error shutting down connection", #[cfg(feature = "http2")] - Kind::Http2 => "http2 error", + Kind::Http2(_) => "http2 error", #[cfg(any(feature = "http1", feature = "http2"))] - Kind::Io => "connection error", + Kind::Io(_) => "connection error", #[cfg(any(feature = "http1", feature = "http2"))] - Kind::User(User::Body) => "error from user's HttpBody stream", + Kind::User(User::Body(_)) => "error from user's HttpBody stream", #[cfg(any(feature = "http1", feature = "http2"))] #[cfg(feature = "server")] - Kind::User(User::MakeService) => "error from user's MakeService", + Kind::User(User::MakeService(_)) => "error from user's MakeService", #[cfg(any(feature = "http1", feature = "http2"))] - Kind::User(User::Service) => "error from user's Service", + Kind::User(User::Service(_)) => "error from user's Service", #[cfg(feature = "http1")] #[cfg(feature = "server")] - Kind::User(User::UnexpectedHeader) => "user sent unexpected header", + Kind::User(User::UnexpectedHeader(_)) => "user sent unexpected header", #[cfg(any(feature = "http1", feature = "http2"))] #[cfg(feature = "client")] - Kind::User(User::UnsupportedVersion) => "request has unsupported HTTP version", + Kind::User(User::UnsupportedVersion(_)) => "request has unsupported HTTP version", #[cfg(any(feature = "http1", feature = "http2"))] #[cfg(feature = "client")] - Kind::User(User::UnsupportedRequestMethod) => "request has unsupported HTTP method", + Kind::User(User::UnsupportedRequestMethod(_)) => "request has unsupported HTTP method", #[cfg(feature = "http1")] #[cfg(feature = "server")] - Kind::User(User::UnsupportedStatusCode) => { + Kind::User(User::UnsupportedStatusCode(_)) => { "response has 1xx status code, not supported by server" } #[cfg(any(feature = "http1", feature = "http2"))] #[cfg(feature = "client")] - Kind::User(User::AbsoluteUriRequired) => "client requires absolute-form URIs", - Kind::User(User::NoUpgrade) => "no upgrade available", + Kind::User(User::AbsoluteUriRequired(_)) => "client requires absolute-form URIs", + Kind::User(User::NoUpgrade(_)) => "no upgrade available", #[cfg(feature = "http1")] - Kind::User(User::ManualUpgrade) => "upgrade expected but low level API in use", + Kind::User(User::ManualUpgrade(_)) => "upgrade expected but low level API in use", #[cfg(feature = "ffi")] Kind::User(User::AbortedByCallback) => "operation aborted by an application callback", } @@ -510,6 +538,13 @@ impl fmt::Display for TimedOut { impl StdError for TimedOut {} +mod sealed { + /// Exists solely to be able to extend error types later. + #[allow(unreachable_pub)] + #[derive(Debug, PartialEq)] + pub struct Sealed; +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/lib.rs b/src/lib.rs index 059f8821c6..898f7ce192 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -79,7 +79,7 @@ mod cfg; #[macro_use] mod common; pub mod body; -mod error; +pub mod error; #[cfg(test)] mod mock; #[cfg(any(feature = "http1", feature = "http2",))] diff --git a/src/proto/h1/dispatch.rs b/src/proto/h1/dispatch.rs index 88e641e9a4..c7127b9073 100644 --- a/src/proto/h1/dispatch.rs +++ b/src/proto/h1/dispatch.rs @@ -686,7 +686,7 @@ mod tests { .expect_err("callback should send error"); match (err.0.kind(), err.1) { - (&crate::error::Kind::Canceled, Some(_)) => (), + (&crate::error::Kind::Canceled(_), Some(_)) => (), other => panic!("expected Canceled, got {:?}", other), } }); diff --git a/src/proto/h2/ping.rs b/src/proto/h2/ping.rs index 105fc69a39..d5ac3dda8b 100644 --- a/src/proto/h2/ping.rs +++ b/src/proto/h2/ping.rs @@ -497,7 +497,7 @@ impl KeepAlive { #[cfg(feature = "runtime")] impl KeepAliveTimedOut { pub(super) fn crate_error(self) -> crate::Error { - crate::Error::new(crate::error::Kind::Http2).with(self) + crate::Error::new_fake_h2(self) } }