diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2020-12-04 15:18:29 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2020-12-04 15:18:29 +0100 |
commit | 03ce0d03f7c092a329fb02b628edebfd64e48580 (patch) | |
tree | 190fe120b6904e078f47e84a3e17c5a7346fd897 | |
parent | 17651193edad67f10f9e139d5b86b2d31665c0a6 (diff) |
Move error implementation to use thiserror
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/errors.rs | 97 |
2 files changed, 25 insertions, 73 deletions
@@ -37,6 +37,7 @@ serde_json = "1.0" tar = "0.4" tokio = "0.2" url = "2.1" +thiserror = "1" [dev-dependencies] env_logger = "0.7" 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, - } - } -} |