docker_sdk/
errors.rs

1//! Representations of various client errors
2
3use hyper::{self, http, StatusCode};
4use serde_json::Error as SerdeError;
5use std::{error::Error as StdError, fmt, string::FromUtf8Error};
6
7use futures_util::io::Error as IoError;
8
9/// Represents the result of all docker operations
10pub type Result<T> = std::result::Result<T, Error>;
11
12#[derive(Debug)]
13pub enum Error {
14    SerdeJsonError(SerdeError),
15    Hyper(hyper::Error),
16    Http(hyper::http::Error),
17    #[allow(clippy::upper_case_acronyms)]
18    IO(IoError),
19    Encoding(FromUtf8Error),
20    InvalidResponse(String),
21    Fault {
22        code: StatusCode,
23        message: String,
24    },
25    ConnectionNotUpgraded,
26}
27
28impl From<SerdeError> for Error {
29    fn from(error: SerdeError) -> Error {
30        Error::SerdeJsonError(error)
31    }
32}
33
34impl From<hyper::Error> for Error {
35    fn from(error: hyper::Error) -> Error {
36        Error::Hyper(error)
37    }
38}
39
40impl From<hyper::http::Error> for Error {
41    fn from(error: hyper::http::Error) -> Error {
42        Error::Http(error)
43    }
44}
45
46impl From<http::uri::InvalidUri> for Error {
47    fn from(error: http::uri::InvalidUri) -> Self {
48        let http_error: http::Error = error.into();
49        http_error.into()
50    }
51}
52
53impl From<IoError> for Error {
54    fn from(error: IoError) -> Error {
55        Error::IO(error)
56    }
57}
58
59impl From<FromUtf8Error> for Error {
60    fn from(error: FromUtf8Error) -> Error {
61        Error::Encoding(error)
62    }
63}
64
65impl fmt::Display for Error {
66    fn fmt(
67        &self,
68        f: &mut fmt::Formatter,
69    ) -> fmt::Result {
70        write!(f, "Docker Error: ")?;
71        match self {
72            Error::SerdeJsonError(ref err) => err.fmt(f),
73            Error::Http(ref err) => err.fmt(f),
74            Error::Hyper(ref err) => err.fmt(f),
75            Error::IO(ref err) => err.fmt(f),
76            Error::Encoding(ref err) => err.fmt(f),
77            Error::InvalidResponse(ref cause) => {
78                write!(f, "Response doesn't have the expected format: {}", cause)
79            }
80            Error::Fault { code, message } => write!(f, "{}: {}", code, message),
81            Error::ConnectionNotUpgraded => write!(
82                f,
83                "expected the docker host to upgrade the HTTP connection but it did not"
84            ),
85        }
86    }
87}
88
89impl StdError for Error {
90    fn source(&self) -> Option<&(dyn StdError + 'static)> {
91        match self {
92            Error::SerdeJsonError(ref err) => Some(err),
93            Error::Http(ref err) => Some(err),
94            Error::IO(ref err) => Some(err),
95            Error::Encoding(e) => Some(e),
96            _ => None,
97        }
98    }
99}