podman_rest_client/api_common/
error.rs

1use std::fmt;
2
3#[derive(thiserror::Error, Debug)]
4pub enum Error {
5    #[error("Api Error: {code} {body}")]
6    Api {
7        code: hyper::StatusCode,
8        body: ApiErrorBody,
9    },
10
11    #[error("URL Parse Error: {0}")]
12    UrlParse(#[from] url::ParseError),
13
14    #[error("Invalid URI: {0}")]
15    InvalidUri(#[from] hyper::http::uri::InvalidUri),
16
17    #[error("HTTP Error: {0}")]
18    HttpError(#[from] hyper::http::Error),
19
20    #[error("Hyper Client Error: {0}")]
21    HyperClient(#[from] hyper_util::client::legacy::Error),
22
23    #[error("Hyper Error: {0}")]
24    Hyper(#[from] hyper::Error),
25
26    #[error("JSON Error: {0}")]
27    SerdeJson(#[from] serde_json::Error),
28
29    #[error("JSON Error: {0}")]
30    SerdeJsonPath(#[from] serde_path_to_error::Error<serde_json::Error>),
31}
32
33#[derive(Debug)]
34pub enum ApiErrorBody {
35    Json(serde_json::Value),
36    Plain(String),
37}
38
39impl From<hyper::body::Bytes> for ApiErrorBody {
40    fn from(bytes: hyper::body::Bytes) -> ApiErrorBody {
41        serde_json::from_slice(&bytes)
42            .map(ApiErrorBody::Json)
43            .unwrap_or_else(|_| ApiErrorBody::Plain(String::from_utf8_lossy(&bytes).to_string()))
44    }
45}
46
47impl fmt::Display for ApiErrorBody {
48    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49        match self {
50            ApiErrorBody::Json(json) => write!(f, "{}", json),
51            ApiErrorBody::Plain(str) => write!(f, "{}", str),
52        }
53    }
54}