Skip to main content

mtjp9_rs_auth0_client/
error.rs

1pub type Result<T, E = Auth0Error> = std::result::Result<T, E>;
2
3/// High‑level errors returned by the Auth0 helpers.
4#[derive(Debug, thiserror::Error)]
5pub enum Auth0Error {
6    /// Low‑level transport / JSON errors.
7    #[error("network/json error: {0}")]
8    Transport(#[from] reqwest::Error),
9
10    /// 400 – The request parameters are invalid.
11    #[error("invalid request: {0}")]
12    InvalidRequest(String),
13
14    /// 401 – Any authentication failure (invalid token, not global, bad JWT sig …).
15    #[error("unauthorized: {0}")]
16    Unauthorized(String),
17
18    /// 403 – Caller authenticated but lacks required scopes.
19    #[error("forbidden / insufficient scope: {0}")]
20    Forbidden(String),
21
22    #[error("conflict status {status}: {body}")]
23    Conflict { status: u16, body: String },
24
25    /// 429 – Too many requests (rate limited).
26    #[error("rate limited: {0}")]
27    TooManyRequests(String),
28
29    /// Any other non‑success HTTP status.
30    #[error("unexpected status {status}: {body}")]
31    UnexpectedResponse { status: u16, body: String },
32}
33
34impl Auth0Error {
35    /// Convert an HTTP response into `Auth0Error` if it isn’t a success.
36    pub async fn from_response(resp: reqwest::Response) -> Self {
37        let status = resp.status();
38        let body = resp.text().await.unwrap_or_default();
39        match status.as_u16() {
40            400 => Self::InvalidRequest(body),
41            401 => Self::Unauthorized(body),
42            403 => Self::Forbidden(body),
43            409 => Self::Conflict {
44                status: status.as_u16(),
45                body,
46            },
47            429 => Self::TooManyRequests(body),
48            code => Self::UnexpectedResponse { status: code, body },
49        }
50    }
51}