autoendpoint/routers/fcm/
error.rs

1use crate::error::ApiErrorKind;
2use crate::routers::RouterError;
3
4use autopush_common::errors::ReportableError;
5use reqwest::StatusCode;
6
7/// Errors that may occur in the Firebase Cloud Messaging router
8#[derive(thiserror::Error, Debug)]
9pub enum FcmError {
10    #[error("Failed to decode the credential settings")]
11    CredentialDecode(#[from] serde_json::Error),
12
13    #[error("Error while building the OAuth client")]
14    OAuthClientBuild(#[source] std::io::Error),
15
16    #[error("Error while retrieving an OAuth token")]
17    OAuthToken(#[from] yup_oauth2::Error),
18
19    #[error("Unable to deserialize FCM response")]
20    DeserializeResponse(#[source] reqwest::Error),
21
22    #[error("Invalid JSON response from FCM")]
23    InvalidResponse(#[source] serde_json::Error, String, StatusCode),
24
25    #[error("Empty response from FCM")]
26    EmptyResponse(StatusCode),
27
28    #[error("No OAuth token was present")]
29    NoOAuthToken,
30
31    #[error("No registration token found for user")]
32    NoRegistrationToken,
33
34    #[error("No app ID found for user")]
35    NoAppId,
36
37    #[error("User has invalid app ID {0}")]
38    InvalidAppId(String),
39
40    #[error("Upstream error, {error_code}: {message}")]
41    Upstream { error_code: String, message: String },
42}
43
44impl FcmError {
45    /// Get the associated HTTP status code
46    pub fn status(&self) -> StatusCode {
47        match self {
48            FcmError::NoRegistrationToken | FcmError::NoAppId | FcmError::InvalidAppId(_) => {
49                StatusCode::GONE
50            }
51
52            FcmError::CredentialDecode(_)
53            | FcmError::OAuthClientBuild(_)
54            | FcmError::OAuthToken(_)
55            | FcmError::NoOAuthToken => StatusCode::INTERNAL_SERVER_ERROR,
56
57            FcmError::DeserializeResponse(_)
58            | FcmError::EmptyResponse(_)
59            | FcmError::InvalidResponse(_, _, _)
60            | FcmError::Upstream { .. } => StatusCode::BAD_GATEWAY,
61        }
62    }
63
64    /// Get the associated error number
65    pub fn errno(&self) -> Option<usize> {
66        match self {
67            FcmError::NoRegistrationToken | FcmError::NoAppId | FcmError::InvalidAppId(_) => {
68                Some(106)
69            }
70
71            _ => None,
72        }
73    }
74}
75
76impl From<FcmError> for ApiErrorKind {
77    fn from(e: FcmError) -> Self {
78        ApiErrorKind::Router(RouterError::Fcm(e))
79    }
80}
81
82impl ReportableError for FcmError {
83    fn is_sentry_event(&self) -> bool {
84        matches!(&self, FcmError::InvalidAppId(_) | FcmError::NoAppId)
85    }
86
87    fn metric_label(&self) -> Option<&'static str> {
88        match &self {
89            FcmError::InvalidAppId(_) | FcmError::NoAppId => Some("notification.bridge.error"),
90            _ => None,
91        }
92    }
93
94    fn extras(&self) -> Vec<(&str, String)> {
95        match self {
96            FcmError::InvalidAppId(appid) => {
97                vec![
98                    ("status", "bad_appid".to_owned()),
99                    ("app_id", appid.to_string()),
100                ]
101            }
102            FcmError::EmptyResponse(status) => {
103                vec![("status", status.to_string())]
104            }
105            FcmError::InvalidResponse(_, body, status) => {
106                vec![("status", status.to_string()), ("body", body.to_owned())]
107            }
108            FcmError::Upstream { error_code, .. } => {
109                vec![("status", error_code.clone())]
110            }
111            _ => vec![],
112        }
113    }
114}