use hyper::StatusCode;
use std::{io, string::FromUtf8Error};
use tokio_util::codec::{LengthDelimitedCodecError, LinesCodecError};
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error(transparent)]
SerdeJson(#[from] serde_json::Error),
#[error(transparent)]
Hyper(#[from] hyper::Error),
#[error(transparent)]
Http(#[from] http::Error),
#[error(transparent)]
IO(#[from] io::Error),
#[error(transparent)]
Encoding(#[from] FromUtf8Error),
#[error("Response doesn't have the expected format: {0}")]
InvalidResponse(String),
#[error("{code}: {message}")]
Fault {
code: StatusCode,
message: String,
},
#[error("expected the docker host to upgrade the HTTP connection but it did not")]
ConnectionNotUpgraded,
#[error("failed to decode bytes")]
Decode,
}
impl From<http::uri::InvalidUri> for Error {
fn from(error: http::uri::InvalidUri) -> Self {
let http_error: http::Error = error.into();
http_error.into()
}
}
impl From<http::header::InvalidHeaderValue> for Error {
fn from(error: http::header::InvalidHeaderValue) -> Self {
let http_error = http::Error::from(error);
http_error.into()
}
}
impl From<LinesCodecError> for Error {
fn from(error: LinesCodecError) -> Self {
match error {
LinesCodecError::MaxLineLengthExceeded => Self::Decode,
LinesCodecError::Io(e) => Self::IO(e),
}
}
}
impl From<LengthDelimitedCodecError> for Error {
fn from(_error: LengthDelimitedCodecError) -> Self {
Self::Decode
}
}