diff options
Diffstat (limited to 'src/errors.rs')
-rw-r--r-- | src/errors.rs | 97 |
1 files changed, 24 insertions, 73 deletions
diff --git a/src/errors.rs b/src/errors.rs index 9d01f91..9e83d82 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,39 +1,37 @@ //! Representations of various client errors +use std::string::FromUtf8Error; + use hyper::{self, http, StatusCode}; use serde_json::Error as SerdeError; -use std::{error::Error as StdError, fmt, string::FromUtf8Error}; - +use thiserror::Error as ThisError; use futures_util::io::Error as IoError; -#[derive(Debug)] +#[derive(ThisError, Debug)] pub enum Error { - SerdeJsonError(SerdeError), - Hyper(hyper::Error), - Http(hyper::http::Error), - IO(IoError), - Encoding(FromUtf8Error), - InvalidResponse(String), - Fault { code: StatusCode, message: String }, - ConnectionNotUpgraded, -} + #[error("{0}")] + SerdeJsonError(#[from] SerdeError), -impl From<SerdeError> for Error { - fn from(error: SerdeError) -> Error { - Error::SerdeJsonError(error) - } -} + #[error("{0}")] + Hyper(#[from] hyper::Error), -impl From<hyper::Error> for Error { - fn from(error: hyper::Error) -> Error { - Error::Hyper(error) - } -} + #[error("{0}")] + Http(#[from] hyper::http::Error), -impl From<hyper::http::Error> for Error { - fn from(error: hyper::http::Error) -> Error { - Error::Http(error) - } + #[error("{0}")] + IO(#[from] IoError), + + #[error("{0}")] + Encoding(#[from] FromUtf8Error), + + #[error("Response doesn't have the expected format: {0}")] + InvalidResponse(String), + + #[error("{code}")] + Fault { code: StatusCode, message: String }, + + #[error("expected the docker host to upgrade the HTTP connection but it did not")] + ConnectionNotUpgraded, } impl From<http::uri::InvalidUri> for Error { @@ -43,50 +41,3 @@ impl From<http::uri::InvalidUri> for Error { } } -impl From<IoError> for Error { - fn from(error: IoError) -> Error { - Error::IO(error) - } -} - -impl From<FromUtf8Error> for Error { - fn from(error: FromUtf8Error) -> Error { - Error::Encoding(error) - } -} - -impl fmt::Display for Error { - fn fmt( - &self, - f: &mut fmt::Formatter, - ) -> fmt::Result { - write!(f, "Docker Error: ")?; - match self { - Error::SerdeJsonError(ref err) => err.fmt(f), - Error::Http(ref err) => err.fmt(f), - Error::Hyper(ref err) => err.fmt(f), - Error::IO(ref err) => err.fmt(f), - Error::Encoding(ref err) => err.fmt(f), - Error::InvalidResponse(ref cause) => { - write!(f, "Response doesn't have the expected format: {}", cause) - } - Error::Fault { code, .. } => write!(f, "{}", code), - Error::ConnectionNotUpgraded => write!( - f, - "expected the docker host to upgrade the HTTP connection but it did not" - ), - } - } -} - -impl StdError for Error { - fn source(&self) -> Option<&(dyn StdError + 'static)> { - match self { - Error::SerdeJsonError(ref err) => Some(err), - Error::Http(ref err) => Some(err), - Error::IO(ref err) => Some(err), - Error::Encoding(e) => Some(e), - _ => None, - } - } -} |