| 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/. */ |
| 4 |
|
| 5 |
use error_support::{ErrorHandling, GetErrorHandling}; |
| 6 |
// reexport logging helpers. |
| 7 |
pub use error_support::{debug, error, info, trace, warn}; |
| 8 |
|
| 9 |
pub type ApiResult<T> = std::result::Result<T, RemoteSettingsError>; |
| 10 |
pub type Result<T> = std::result::Result<T, Error>; |
| 11 |
|
| 12 |
/// Public error class, this is what we return to consumers |
| 13 |
#[derive(Debug, thiserror::Error, uniffi::Error)] |
| 14 |
pub enum RemoteSettingsError { |
| 15 |
/// Network error while making a remote settings request |
| 16 |
#[error("Remote settings unexpected error: {reason}")] |
| 17 |
Network { reason: String }, |
| 18 |
|
| 19 |
/// The server has asked the client to backoff. |
| 20 |
#[error("Server asked the client to back off ({seconds} seconds remaining)")] |
| 21 |
Backoff { seconds: u64 }, |
| 22 |
|
| 23 |
#[error("Remote settings error: {reason}")] |
| 24 |
Other { reason: String }, |
| 25 |
} |
| 26 |
|
| 27 |
/// Internal error class, this is what we use inside this crate |
| 28 |
#[derive(Debug, thiserror::Error)] |
| 29 |
pub enum Error { |
| 30 |
#[error("Error opening database: {0}")] |
| 31 |
OpenDatabase(#[from] sql_support::open_database::Error), |
| 32 |
#[error("JSON Error: {0}")] |
| 33 |
JSONError(#[from] serde_json::Error), |
| 34 |
#[error("Error writing downloaded attachment: {0}")] |
| 35 |
AttachmentFileError(std::io::Error), |
| 36 |
#[error("Error creating storage dir: {0}")] |
| 37 |
CreateDirError(std::io::Error), |
| 38 |
/// An error has occurred while sending a request. |
| 39 |
#[error("Error sending request: {0}")] |
| 40 |
RequestError(#[from] viaduct::Error), |
| 41 |
/// An error has occurred while parsing an URL. |
| 42 |
#[error("Error parsing URL: {0}")] |
| 43 |
UrlParsingError(#[from] url::ParseError), |
| 44 |
/// The server has asked the client to backoff. |
| 45 |
#[error("Server asked the client to back off ({0} seconds remaining)")] |
| 46 |
BackoffError(u64), |
| 47 |
/// The server returned an error code or the response was unexpected. |
| 48 |
#[error("Error in network response: {0}")] |
| 49 |
ResponseError(String), |
| 50 |
#[error("This server doesn't support attachments")] |
| 51 |
AttachmentsUnsupportedError, |
| 52 |
#[error("Error configuring client: {0}")] |
| 53 |
ConfigError(String), |
| 54 |
#[error("Database error: {0}")] |
| 55 |
DatabaseError(#[from] rusqlite::Error), |
| 56 |
#[error("Database closed")] |
| 57 |
DatabaseClosed, |
| 58 |
#[error("No attachment in given record: {0}")] |
| 59 |
RecordAttachmentMismatchError(String), |
| 60 |
#[error("Incomplete signature data: {0}")] |
| 61 |
IncompleteSignatureDataError(String), |
| 62 |
#[cfg(feature = "signatures")] |
| 63 |
#[error("Data could not be serialized: {0}")] |
| 64 |
SerializationError(#[from] canonical_json::CanonicalJSONError), |
| 65 |
#[cfg(feature = "signatures")] |
| 66 |
#[error("Signature could not be verified: {0}")] |
| 67 |
SignatureError(#[from] rc_crypto::Error), |
| 68 |
} |
| 69 |
|
| 70 |
// 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. |
| 72 |
impl GetErrorHandling for Error { |
| 73 |
type ExternalError = RemoteSettingsError; |
| 74 |
|
| 75 |
fn get_error_handling(&self) -> ErrorHandling<Self::ExternalError> { |
| 76 |
match self { |
| 77 |
// Network errors are expected to happen in practice. Let's log, but not report them. |
| 78 |
Self::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. |
| 87 |
Self::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 |
} |