remote_settings/
error.rs

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use error_support::{ErrorHandling, GetErrorHandling};

pub type ApiResult<T> = std::result::Result<T, RemoteSettingsError>;
pub type Result<T> = std::result::Result<T, Error>;

/// Public error class, this is what we return to consumers
#[derive(Debug, thiserror::Error, uniffi::Error)]
pub enum RemoteSettingsError {
    /// Network error while making a remote settings request
    #[error("Remote settings unexpected error: {reason}")]
    Network { reason: String },

    /// The server has asked the client to backoff.
    #[error("Server asked the client to back off ({seconds} seconds remaining)")]
    Backoff { seconds: u64 },

    #[error("Remote settings error: {reason}")]
    Other { reason: String },
}

/// Internal error class, this is what we use inside this crate
#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("JSON Error: {0}")]
    JSONError(#[from] serde_json::Error),
    #[error("Error writing downloaded attachment: {0}")]
    FileError(#[from] std::io::Error),
    /// An error has occurred while sending a request.
    #[error("Error sending request: {0}")]
    RequestError(#[from] viaduct::Error),
    /// An error has occurred while parsing an URL.
    #[error("Error parsing URL: {0}")]
    UrlParsingError(#[from] url::ParseError),
    /// The server has asked the client to backoff.
    #[error("Server asked the client to back off ({0} seconds remaining)")]
    BackoffError(u64),
    /// The server returned an error code or the response was unexpected.
    #[error("Error in network response: {0}")]
    ResponseError(String),
    #[error("This server doesn't support attachments")]
    AttachmentsUnsupportedError,
    #[error("Error configuring client: {0}")]
    ConfigError(String),
    #[error("Database error: {0}")]
    DatabaseError(#[from] rusqlite::Error),
    #[error("No attachment in given record: {0}")]
    RecordAttachmentMismatchError(String),
}

// Define how our internal errors are handled and converted to external errors
// See `support/error/README.md` for how this works, especially the warning about PII.
impl GetErrorHandling for Error {
    type ExternalError = RemoteSettingsError;

    fn get_error_handling(&self) -> ErrorHandling<Self::ExternalError> {
        match self {
            // Network errors are expected to happen in practice.  Let's log, but not report them.
            Self::RequestError(viaduct::Error::NetworkError(e)) => {
                ErrorHandling::convert(RemoteSettingsError::Network {
                    reason: e.to_string(),
                })
                .log_warning()
            }
            // Backoff error shouldn't happen in practice, so let's report them for now.
            // If these do happen in practice and we decide that there is a valid reason for them,
            // then consider switching from reporting to Sentry to counting in Glean.
            Self::BackoffError(seconds) => {
                ErrorHandling::convert(RemoteSettingsError::Backoff { seconds: *seconds })
                    .report_error("suggest-backoff")
            }
            _ => ErrorHandling::convert(RemoteSettingsError::Other {
                reason: self.to_string(),
            })
            .report_error("logins-unexpected"),
        }
    }
}