raystack/
err.rs

1use crate::auth::AuthError;
2use crate::grid::{Grid, ParseJsonGridError};
3use thiserror::Error;
4
5impl Error {
6    /// Return true if this error encapsulates a Haystack error grid.
7    pub fn is_grid(&self) -> bool {
8        matches!(self, Self::Grid { .. })
9    }
10
11    /// Return a reference to the Haystack error grid encapsulated by this
12    /// error, if this error was caused by a Haystack error grid.
13    pub fn grid(&self) -> Option<&Grid> {
14        match self {
15            Self::Grid { err_grid } => Some(err_grid),
16            _ => None,
17        }
18    }
19
20    /// Return the Haystack error grid encapsulated by this error, if this
21    /// error was caused by a Haystack error grid.
22    pub fn into_grid(self) -> Option<Grid> {
23        match self {
24            Self::Grid { err_grid } => Some(err_grid),
25            _ => None,
26        }
27    }
28}
29
30/// Describes the kinds of errors that can occur in this crate.
31#[derive(Debug, Error)]
32pub enum Error {
33    /// The grid contained error information from the server.
34    #[error("Server returned an error grid")]
35    Grid {
36        /// The grid which caused this error.
37        err_grid: Grid,
38    },
39    /// An error which originated from the underlying HTTP library.
40    #[error("Error occurred in the underlying HTTP library")]
41    Http {
42        #[from]
43        err: reqwest::Error,
44    },
45    /// An error related to parsing a `Grid` from a JSON value.
46    #[error("Could not parse JSON as a Haystack grid")]
47    ParseJsonGrid(#[from] ParseJsonGridError),
48    /// An error caused by an invalid time zone.
49    #[error("Not a valid time zone: {err_time_zone}")]
50    TimeZone {
51        /// The time zone which caused the error.
52        err_time_zone: String,
53    },
54    /// An error occurred when trying to obtain a new auth token from
55    /// the server.
56    #[error("Could not obtain a new auth token from the server")]
57    UpdateAuthToken(#[from] crate::auth::AuthError),
58}
59
60/// Errors that can occur when creating a new `SkySparkClient`.
61#[derive(Debug, Error)]
62pub enum NewSkySparkClientError {
63    /// An error which occurred during the authentication process.
64    #[error("Error occurred during authentication")]
65    Auth(#[from] AuthError),
66    /// An error caused by an invalid SkySpark project url.
67    #[error("The SkySpark URL is invalid: {msg}")]
68    Url { msg: String },
69}
70
71impl NewSkySparkClientError {
72    pub(crate) fn url(msg: &str) -> Self {
73        NewSkySparkClientError::Url { msg: msg.into() }
74    }
75}