1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//! Types relative to error handling.

use alcoholic_jwt::ValidationError;
use reqwest::Error as ReqwestError;
use serde::Deserialize;
use serde_json::Error as SerdeJsonError;
use thiserror::Error as ThisError;

use crate::users::UserError;

/// The error type which represent an error returned by the Auth0 API.
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Auth0ApiError {
    pub status_code: i32,
    pub error: String,
    pub message: String,
    pub error_code: Option<String>,
}

/// The error type which is returned if some error occurs duging a request.
#[derive(Debug, ThisError)]
pub enum Error {
    #[error("Missing kid in JWT")]
    JwtMissingKid,
    #[error("Invalid JWT: {0}")]
    InvalidJwt(#[from] ValidationError),
    #[error("Serialization error: {0}")]
    Serialization(#[from] SerdeJsonError),
    #[error("Reqwest error: {0}")]
    Http(#[from] ReqwestError),
    #[error("Too many requests")]
    TooManyRequests,
    #[error("Unauthorized")]
    Unauthorized,
    #[error("User error: {0}")]
    User(#[from] UserError),
    #[error("Unimplemented")]
    Unimplemented,
    #[error("Unknown error: {0}")]
    Unknown(String),
    #[error("Invalid response body")]
    InvalidResponseBody,
    #[error("Invalid username")]
    InvalidUsername,
    #[error("Invalid password")]
    InvalidPassword,
}

pub type Auth0Result<T> = Result<T, Error>;