1use std::fmt::{Display, Formatter};
2
3#[derive(Debug)]
4pub enum Error {
5 BuilderMissingField(&'static str),
6 BuilderConflict(&'static str),
7 Io(std::io::Error),
8 Http(reqwest::Error),
9 Json(serde_json::Error),
10 AuthUnavailable,
11 EmptyResponse,
12 RequestFailed {
13 status: reqwest::StatusCode,
14 body: String,
15 },
16}
17
18impl Display for Error {
19 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
20 match self {
21 Self::BuilderMissingField(field) => {
22 write!(f, "missing required builder field: {field}")
23 }
24 Self::BuilderConflict(message) => write!(f, "invalid builder configuration: {message}"),
25 Self::Io(error) => write!(f, "io error: {error}"),
26 Self::Http(error) => write!(f, "http error: {error}"),
27 Self::Json(error) => write!(f, "json error: {error}"),
28 Self::AuthUnavailable => write!(f, "authentication token is unavailable"),
29 Self::EmptyResponse => write!(f, "received empty response from server"),
30 Self::RequestFailed { status, body } => {
31 write!(f, "request failed with status {status}: {body}")
32 }
33 }
34 }
35}
36
37impl From<std::io::Error> for Error {
38 fn from(value: std::io::Error) -> Self {
39 Self::Io(value)
40 }
41}
42
43impl From<reqwest::Error> for Error {
44 fn from(value: reqwest::Error) -> Self {
45 Self::Http(value)
46 }
47}
48
49impl From<serde_json::Error> for Error {
50 fn from(value: serde_json::Error) -> Self {
51 Self::Json(value)
52 }
53}
54
55impl std::error::Error for Error {
56 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
57 match self {
58 Self::Io(error) => Some(error),
59 Self::Http(error) => Some(error),
60 Self::Json(error) => Some(error),
61 _ => None,
62 }
63 }
64}