1use error_support::{ErrorHandling, GetErrorHandling};
6pub use error_support::{debug, error, info, trace, warn};
8
9pub type ApiResult<T> = std::result::Result<T, RemoteSettingsError>;
10pub type Result<T> = std::result::Result<T, Error>;
11
12#[derive(Debug, thiserror::Error, uniffi::Error)]
14pub enum RemoteSettingsError {
15 #[error("Remote settings unexpected error: {reason}")]
17 Network { reason: String },
18
19 #[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#[derive(Debug, thiserror::Error)]
29pub 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 #[error("Error sending request: {0}")]
40 RequestError(#[from] viaduct::Error),
41 #[error("Error parsing URL: {0}")]
43 UrlParsingError(#[from] url::ParseError),
44 #[error("Server asked the client to back off ({0} seconds remaining)")]
46 BackoffError(u64),
47 #[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
70impl GetErrorHandling for Error {
73 type ExternalError = RemoteSettingsError;
74
75 fn get_error_handling(&self) -> ErrorHandling<Self::ExternalError> {
76 match self {
77 Self::RequestError(viaduct::Error::NetworkError(e)) => {
79 ErrorHandling::convert(RemoteSettingsError::Network {
80 reason: e.to_string(),
81 })
82 .log_warning()
83 }
84 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}