up_api/v1/
error.rs

1use std::fmt;
2
3use serde::Deserialize;
4
5#[derive(Debug)]
6/// Primary error type for requests made through `up_api::Client`.
7pub enum Error {
8    /// Represents cases where the URL could not be parsed correctly.
9    UrlParse(url::ParseError),
10    /// Represents an error in making the HTTP request.
11    Request(reqwest::Error),
12    /// Represents errors from the API (i.e. a non `2XX` response code).
13    Api(ErrorResponse),
14    /// Represents an error in deserializing JSON to the required structures.
15    /// Occurences of this error should be treated as a bug in the library.
16    Json(serde_json::Error),
17    /// Represents an error in reading the body from the HTTP response.
18    /// Occurences of this error should be treated as a bug in the library. 
19    BodyRead(reqwest::Error),
20    /// Represents an error serializing the data to be sent to the API.
21    /// Occurences of this error should be treated as a bug in the library.
22    Serialize(serde_json::Error),
23}
24
25impl fmt::Display for Error {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
27        match self {
28            Self::UrlParse(val) => write!(f,
29                "Failed to parse the URL before making the request: {:?}",
30                val,
31            ),
32            Self::Request(val) => write!(f,
33                "Failed to make the HTTP request to the API endpoint: {:?}",
34                val,
35            ),
36            Self::Api(val) => write!(f,
37                "The API returned an error response: {:?}",
38                val,
39            ),
40            Self::Json(val) => write!(f,
41                "Failed to deserialize the returned JSON to the correct format: {:?}",
42                val,
43            ),
44            Self::BodyRead(val) => write!(f,
45                "Failed to read the response body as a UTF-8 string: {:?}",
46                val,
47            ),
48            Self::Serialize(val) => write!(f,
49                "Failed to serialize the request data: {:?}",
50                val,
51            ),
52        }
53    }
54}
55
56impl std::error::Error for Error {}
57
58#[derive(Deserialize, Debug)]
59pub struct ErrorResponse {
60    /// The list of errors returned in this response.
61    pub errors: Vec<ErrorObject>,
62}
63
64#[derive(Deserialize, Debug)]
65pub struct ErrorObject {
66    /// The HTTP status code associated with this error. The status indicates
67    /// the broad type of error according to HTTP semantics.
68    pub status: String,
69    /// A short description of this error. This should be stable across
70    /// multiple occurrences of this type of error and typically expands on the
71    /// reason for the status code.
72    pub title: String,
73    /// A detailed description of this error. This should be considered unique
74    /// to individual occurrences of an error and subject to change. It is
75    /// useful for debugging purposes.
76    pub detail: String,
77    /// If applicable, location in the request that this error relates to. This
78    /// may be a parameter in the query string, or a an attribute in the
79    /// request body.
80    pub source: Option<Source>,
81}
82
83#[derive(Deserialize, Debug)]
84pub struct Source {
85    /// If this error relates to a query parameter, the name of the parameter.
86    pub parameter: Option<String>,
87    /// If this error relates to an attribute in the request body, a rfc-6901
88    /// JSON pointer to the attribute.
89    pub pointer: Option<String>
90}