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. Occurances of this
15 /// 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. Occurances of this
18 /// 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. Occurances of this
21 /// 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, "Failed to parse the URL before making the request: {:?}", val),
29 Self::Request(val) => write!(f, "Failed to make the HTTP request to the API endpoint: {:?}", val),
30 Self::Api(val) => write!(f, "The API returned an error response: {:?}", val),
31 Self::Json(val) => write!(f, "Failed to deserialize the returned JSON to the correct format: {:?}", val),
32 Self::BodyRead(val) => write!(f, "Failed to read the response body as a UTF-8 string: {:?}", val),
33 Self::Serialize(val) => write!(f, "Failed to serialize the request data: {:?}", val),
34 }
35 }
36}
37
38impl std::error::Error for Error {}
39
40#[derive(Deserialize, Debug)]
41pub struct ErrorResponse {
42 /// The list of errors returned in this response.
43 pub errors : Vec<ErrorObject>,
44}
45
46#[derive(Deserialize, Debug)]
47pub struct ErrorObject {
48 /// The HTTP status code associated with this error. The status indicates the broad type of error according to HTTP semantics.
49 pub status : String,
50 /// A short description of this error. This should be stable across multiple occurrences of this type of error and typically expands on the reason for the status code.
51 pub title : String,
52 /// A detailed description of this error. This should be considered unique to individual occurrences of an error and subject to change. It is useful for debugging purposes.
53 pub detail : String,
54 /// If applicable, location in the request that this error relates to. This may be a parameter in the query string, or a an attribute in the request body.
55 pub source : Option<Source>,
56}
57
58#[derive(Deserialize, Debug)]
59pub struct Source {
60 /// If this error relates to a query parameter, the name of the parameter.
61 pub parameter : Option<String>,
62 /// If this error relates to an attribute in the request body, a rfc-6901 JSON pointer to the attribute.
63 pub pointer : Option<String>
64}