openleadr_client/
error.rs

1use reqwest::StatusCode;
2
3/// Errors that can occur using the [`Client`](crate::Client)
4#[derive(Debug)]
5#[allow(missing_docs)]
6pub enum Error {
7    Reqwest(reqwest::Error),
8    Serde(serde_json::Error),
9    UrlParseError(url::ParseError),
10    Problem(openleadr_wire::problem::Problem),
11    AuthProblem(openleadr_wire::oauth::OAuthError),
12    OAuthTokenNotBearer,
13    ObjectNotFound,
14    DuplicateObject,
15    /// Error if you try
16    /// to create an event underneath a program
17    /// where the [`program_id`](crate::EventContent::program_id)
18    /// in the [`EventContent`](crate::EventContent)
19    /// does not match the program ID of the [`ProgramClient`](crate::ProgramClient),
20    /// for example.
21    InvalidParentObject,
22    InvalidInterval,
23}
24
25impl Error {
26    /// Checks if the [`Problem`](openleadr_wire::problem::Problem) response of the VTN is a
27    /// `409 Conflict` HTTP status code.
28    pub fn is_conflict(&self) -> bool {
29        match self {
30            Error::Problem(openleadr_wire::problem::Problem { status, .. }) => {
31                *status == StatusCode::CONFLICT
32            }
33            _ => false,
34        }
35    }
36
37    #[allow(missing_docs)]
38    pub fn is_not_found(&self) -> bool {
39        match self {
40            Error::Problem(openleadr_wire::problem::Problem { status, .. }) => {
41                *status == StatusCode::NOT_FOUND
42            }
43            _ => false,
44        }
45    }
46}
47
48impl From<reqwest::Error> for Error {
49    fn from(err: reqwest::Error) -> Self {
50        Error::Reqwest(err)
51    }
52}
53
54impl From<serde_json::Error> for Error {
55    fn from(err: serde_json::Error) -> Self {
56        Error::Serde(err)
57    }
58}
59
60impl From<url::ParseError> for Error {
61    fn from(err: url::ParseError) -> Self {
62        Error::UrlParseError(err)
63    }
64}
65
66impl From<openleadr_wire::problem::Problem> for Error {
67    fn from(err: openleadr_wire::problem::Problem) -> Self {
68        Error::Problem(err)
69    }
70}
71
72impl From<openleadr_wire::oauth::OAuthError> for Error {
73    fn from(err: openleadr_wire::oauth::OAuthError) -> Self {
74        Error::AuthProblem(err)
75    }
76}
77
78impl std::fmt::Display for Error {
79    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
80        match self {
81            Error::Reqwest(err) => write!(f, "Reqwest error: {err}"),
82            Error::Serde(err) => write!(f, "Serde error: {err}"),
83            Error::UrlParseError(err) => write!(f, "URL parse error: {err}"),
84            Error::Problem(err) => write!(f, "OpenADR Problem: {err:?}"),
85            Error::AuthProblem(err) => write!(f, "Authentication problem: {err:?}"),
86            Error::ObjectNotFound => write!(f, "Object not found"),
87            Error::DuplicateObject => write!(f, "Found more than one object matching the filter"),
88            Error::InvalidParentObject => write!(f, "Invalid parent object"),
89            Error::InvalidInterval => write!(f, "Invalid interval specified"),
90            Error::OAuthTokenNotBearer => write!(f, "OAuth token received is not a Bearer token"),
91        }
92    }
93}
94
95impl std::error::Error for Error {}
96
97pub(crate) type Result<T> = std::result::Result<T, Error>;