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

use 'thiserror' to define 'Error' #319

Open
wants to merge 1 commit 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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ url = "2.1"
# XXX: This is a temporary dependency for the reexport! macro in lib.rs. Remove
# me before 0.9.0 is released.
paste = "1.0"
thiserror = "1.0.30"

[dev-dependencies]
env_logger = "0.8"
Expand Down
95 changes: 22 additions & 73 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,42 @@

use hyper::{self, http, StatusCode};
use serde_json::Error as SerdeError;
use std::{error::Error as StdError, fmt, string::FromUtf8Error};
use std::string::FromUtf8Error;

use futures_util::io::Error as IoError;

/// Represents the result of all docker operations
pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug)]
#[derive(Debug, thiserror::Error)]
pub enum Error {
SerdeJsonError(SerdeError),
Hyper(hyper::Error),
Http(hyper::http::Error),
#[error(transparent)]
SerdeJsonError(#[from] SerdeError),

#[error(transparent)]
Hyper(#[from] hyper::Error),

#[error(transparent)]
Http(#[from] hyper::http::Error),

#[allow(clippy::upper_case_acronyms)]
IO(IoError),
Encoding(FromUtf8Error),
#[error(transparent)]
IO(#[from] IoError),

#[error(transparent)]
Encoding(#[from] FromUtf8Error),

#[error("Response doesn't have the expected format: {0}")]
InvalidResponse(String),

#[error("{code}: {message}")]
Fault {
code: StatusCode,
message: String,
},
ConnectionNotUpgraded,
}

impl From<SerdeError> for Error {
fn from(error: SerdeError) -> Error {
Error::SerdeJsonError(error)
}
}

impl From<hyper::Error> for Error {
fn from(error: hyper::Error) -> Error {
Error::Hyper(error)
}
}

impl From<hyper::http::Error> for Error {
fn from(error: hyper::http::Error) -> Error {
Error::Http(error)
}
#[error("expected the docker host to upgrade the HTTP connection but it did not")]
ConnectionNotUpgraded,
}

impl From<http::uri::InvalidUri> for Error {
Expand All @@ -49,51 +46,3 @@ impl From<http::uri::InvalidUri> for Error {
http_error.into()
}
}

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, message } => write!(f, "{}: {}", code, message),
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,
}
}
}