freedom_api/
error.rs

1//! Error and Result types for Freedom API
2use serde::Serialize;
3
4/// Result type for the API
5pub type Result<T> = std::result::Result<T, Error>;
6
7/// The combined error type for the client builder and for API errors
8#[derive(Debug, Clone, thiserror::Error, PartialEq, Eq, Serialize)]
9pub enum Error {
10    #[error("Failed to get valid response from server: {0}")]
11    Response(String),
12
13    #[error("Failed to deserialize the response: {0}")]
14    Deserialization(String),
15
16    #[error("Paginated item failed deserialized: {0}")]
17    PaginationItemDeserialization(String),
18
19    // We do not place the time::error::Error in the error since it includes std::io::Error which
20    // does not implement Clone, PartialEq, or Eq
21    #[error("Time parsing error: {0}")]
22    TimeFormatError(String),
23
24    #[error("Failed to parse item into valid URI: {0}")]
25    InvalidUri(String),
26
27    #[error("Failed to retrieve the HATEOAS URI: {0}")]
28    MissingUri(&'static str),
29
30    #[error("Failed to parse the final segment of the path as an ID.")]
31    InvalidId,
32}
33
34impl Error {
35    /// Shorthand for creating a runtime pagination error
36    pub(crate) fn pag_item(s: String) -> Self {
37        Self::PaginationItemDeserialization(s)
38    }
39}
40
41impl From<reqwest::Error> for Error {
42    fn from(value: reqwest::Error) -> Self {
43        Error::Response(value.to_string())
44    }
45}
46
47impl From<serde_json::Error> for Error {
48    fn from(value: serde_json::Error) -> Self {
49        Error::Deserialization(value.to_string())
50    }
51}
52
53impl From<time::error::Error> for Error {
54    fn from(value: time::error::Error) -> Self {
55        Error::TimeFormatError(value.to_string())
56    }
57}
58
59impl From<time::error::Format> for Error {
60    fn from(value: time::error::Format) -> Self {
61        Error::TimeFormatError(value.to_string())
62    }
63}
64
65impl From<url::ParseError> for Error {
66    fn from(value: url::ParseError) -> Self {
67        Self::InvalidUri(value.to_string())
68    }
69}