1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
45use error_support::{ErrorHandling, GetErrorHandling};
6// reexport logging helpers.
7pub use error_support::{debug, error, info, trace, warn};
89pub type ApiResult<T> = std::result::Result<T, RemoteSettingsError>;
10pub type Result<T> = std::result::Result<T, Error>;
1112/// Public error class, this is what we return to consumers
13#[derive(Debug, thiserror::Error, uniffi::Error)]
14pub enum RemoteSettingsError {
15/// Network error while making a remote settings request
16#[error("Remote settings unexpected error: {reason}")]
17Network { reason: String },
1819/// The server has asked the client to backoff.
20#[error("Server asked the client to back off ({seconds} seconds remaining)")]
21Backoff { seconds: u64 },
2223#[error("Remote settings error: {reason}")]
24Other { reason: String },
25}
2627/// Internal error class, this is what we use inside this crate
28#[derive(Debug, thiserror::Error)]
29pub enum Error {
30#[error("Error opening database: {0}")]
31OpenDatabase(#[from] sql_support::open_database::Error),
32#[error("JSON Error: {0}")]
33JSONError(#[from] serde_json::Error),
34#[error("Error writing downloaded attachment: {0}")]
35AttachmentFileError(std::io::Error),
36#[error("Error creating storage dir: {0}")]
37CreateDirError(std::io::Error),
38/// An error has occurred while sending a request.
39#[error("Error sending request: {0}")]
40RequestError(#[from] viaduct::Error),
41/// An error has occurred while parsing an URL.
42#[error("Error parsing URL: {0}")]
43UrlParsingError(#[from] url::ParseError),
44/// The server has asked the client to backoff.
45#[error("Server asked the client to back off ({0} seconds remaining)")]
46BackoffError(u64),
47/// The server returned an error code or the response was unexpected.
48#[error("Error in network response: {0}")]
49ResponseError(String),
50#[error("This server doesn't support attachments")]
51AttachmentsUnsupportedError,
52#[error("Error configuring client: {0}")]
53ConfigError(String),
54#[error("Database error: {0}")]
55DatabaseError(#[from] rusqlite::Error),
56#[error("Database closed")]
57DatabaseClosed,
58#[error("No attachment in given record: {0}")]
59RecordAttachmentMismatchError(String),
60#[error("Incomplete signature data: {0}")]
61IncompleteSignatureDataError(String),
62#[cfg(feature = "signatures")]
63 #[error("Data could not be serialized: {0}")]
64SerializationError(#[from] canonical_json::CanonicalJSONError),
65#[cfg(feature = "signatures")]
66 #[error("Signature could not be verified: {0}")]
67SignatureError(#[from] rc_crypto::Error),
68}
6970// Define how our internal errors are handled and converted to external errors
71// See `support/error/README.md` for how this works, especially the warning about PII.
72impl GetErrorHandling for Error {
73type ExternalError = RemoteSettingsError;
7475fn get_error_handling(&self) -> ErrorHandling<Self::ExternalError> {
76match self {
77// Network errors are expected to happen in practice. Let's log, but not report them.
78Self::RequestError(viaduct::Error::NetworkError(e)) => {
79 ErrorHandling::convert(RemoteSettingsError::Network {
80 reason: e.to_string(),
81 })
82 .log_warning()
83 }
84// Backoff error shouldn't happen in practice, so let's report them for now.
85 // If these do happen in practice and we decide that there is a valid reason for them,
86 // then consider switching from reporting to Sentry to counting in Glean.
87Self::BackoffError(seconds) => {
88 ErrorHandling::convert(RemoteSettingsError::Backoff { seconds: *seconds })
89 .report_error("suggest-backoff")
90 }
91_ => ErrorHandling::convert(RemoteSettingsError::Other {
92 reason: self.to_string(),
93 })
94 .report_error("remote-settings-unexpected"),
95 }
96 }
97}