apollo_client/
errors.rs

1//! Crate level errors.
2
3use http::StatusCode;
4use reqwest::Response;
5use std::str::Utf8Error;
6
7/// Crate level result.
8pub type ApolloClientResult<T> = Result<T, ApolloClientError>;
9
10/// Crate level error.
11#[derive(thiserror::Error, Debug)]
12pub enum ApolloClientError {
13    #[error(transparent)]
14    Utf8(#[from] Utf8Error),
15
16    #[error(transparent)]
17    Http(#[from] http::Error),
18
19    #[error(transparent)]
20    Reqwest(#[from] reqwest::Error),
21
22    #[error(transparent)]
23    SerdeJson(#[from] serde_json::Error),
24
25    #[cfg(feature = "conf")]
26    #[cfg_attr(docsrs, doc(cfg(feature = "conf")))]
27    #[error(transparent)]
28    IniParse(#[from] ini::ParseError),
29
30    #[error(transparent)]
31    ApolloResponse(#[from] ApolloResponseError),
32
33    #[error("this URL is cannot-be-a-base")]
34    UrlCannotBeABase,
35}
36
37/// Apollo api response error, when http status is not success.
38#[derive(thiserror::Error, Debug)]
39#[error(r#"error occurred when apollo response, status: {status}, body: "{body}""#)]
40pub struct ApolloResponseError {
41    /// Http response status.
42    pub status: StatusCode,
43    /// Http response body, mainly the error reason.
44    pub body: String,
45}
46
47impl ApolloResponseError {
48    pub(crate) async fn from_response(response: Response) -> Result<Response, Self> {
49        if response.status().is_success() {
50            Ok(response)
51        } else {
52            Err(Self {
53                status: response.status(),
54                body: response.text().await.unwrap_or_default(),
55            })
56        }
57    }
58}