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
51
52
53
54
55
56
use crate::error::ApiErrorKind;
use crate::routers::RouterError;
use actix_web::http::StatusCode;

/// Errors that may occur in the Firebase Cloud Messaging router
#[derive(thiserror::Error, Clone, Debug)]
pub enum StubError {
    #[error("Failed to decode the credential settings")]
    Credential,

    #[error("Invalid JSON response from Test")]
    InvalidResponse,

    #[error("General error")]
    General(String),

    #[error("Missing User error")]
    Missing,
}

impl StubError {
    /// Get the associated HTTP status code
    pub fn status(&self) -> StatusCode {
        match self {
            StubError::Missing => StatusCode::GONE,

            StubError::Credential | StubError::General(_) => StatusCode::INTERNAL_SERVER_ERROR,

            StubError::InvalidResponse => StatusCode::BAD_GATEWAY,
        }
    }

    /// Get the associated error number
    pub fn errno(&self) -> Option<usize> {
        match self {
            StubError::Missing => Some(106),

            _ => None,
        }
    }

    pub fn extras(&self) -> Vec<(&str, String)> {
        match self {
            StubError::General(status) => {
                vec![("status", status.to_string())]
            }
            _ => vec![],
        }
    }
}

impl From<StubError> for ApiErrorKind {
    fn from(e: StubError) -> Self {
        ApiErrorKind::Router(RouterError::Stub(e))
    }
}