1use reqwest::header::HeaderMap;
2use std::fmt;
3
4pub type Result<T> = std::result::Result<T, Error>;
5
6#[derive(Debug, Clone)]
8pub struct ApiError {
9 pub status: u16,
10 pub code: String,
11 pub message: String,
12 pub request_id: String,
13 pub operation: String,
14 pub headers: HeaderMap,
15}
16
17#[derive(Debug, Clone)]
18#[non_exhaustive]
19pub enum Error {
20 Configuration(String),
21 Protocol(String),
22 Authentication { status: Option<u16> },
23 Api(Box<ApiError>),
24 Transport,
25 Timeout,
26 AmbiguousReference,
27}
28
29impl fmt::Display for Error {
30 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31 match self {
32 Self::Configuration(s) => write!(f, "Invalid configuration: {s}"),
33 Self::Protocol(s) => write!(f, "Invalid API response: {s}"),
34 Self::Authentication { status } => {
35 write!(f, "Authentication failed (status {status:?})")
36 }
37 Self::Api(e) => write!(
38 f,
39 "{}: HTTP {} {}: {} (request {})",
40 e.operation, e.status, e.code, e.message, e.request_id
41 ),
42 Self::Transport => f.write_str("HTTP transport failed"),
43 Self::Timeout => f.write_str("HTTP request timed out"),
44 Self::AmbiguousReference => f.write_str("More than one resource matches the reference"),
45 }
46 }
47}
48impl std::error::Error for Error {}
49
50impl From<reqwest::Error> for Error {
51 fn from(e: reqwest::Error) -> Self {
52 if e.is_timeout() {
53 Self::Timeout
54 } else {
55 Self::Transport
56 }
57 }
58}
59impl From<serde_json::Error> for Error {
60 fn from(_: serde_json::Error) -> Self {
61 Self::Protocol("JSON does not match the expected schema".into())
62 }
63}
64
65pub(crate) fn invalid(message: &str) -> Error {
66 Error::Configuration(message.into())
67}