Backed out changeset 0ac3aa2e9c97 (bug 1910869) for causing failures at 2d.text.draw...
[gecko.git] / third_party / rust / suggest / src / error.rs
blobf99ff3658b4e4968411c87d4ebd2fc9a7d501b6d
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  */
6 use error_support::{ErrorHandling, GetErrorHandling};
7 use remote_settings::RemoteSettingsError;
9 /// A list of errors that are internal to the component. This is the error
10 /// type for private and crate-internal methods, and is never returned to the
11 /// application.
12 #[derive(Debug, thiserror::Error)]
13 pub enum Error {
14     #[error("Error opening database: {0}")]
15     OpenDatabase(#[from] sql_support::open_database::Error),
17     #[error("Error executing SQL: {inner} (context: {context})")]
18     Sql {
19         inner: rusqlite::Error,
20         context: String,
21     },
23     #[error("JSON error: {0}")]
24     Json(#[from] serde_json::Error),
26     #[error("Error from Remote Settings: {0}")]
27     RemoteSettings(#[from] RemoteSettingsError),
29     #[error("Remote settings record is missing an attachment (id: u64)")]
30     MissingAttachment(String),
32     #[error("Operation interrupted")]
33     Interrupted(#[from] interrupt_support::Interrupted),
35     #[error("SuggestStoreBuilder {0}")]
36     SuggestStoreBuilder(String),
39 impl Error {
40     fn sql(e: rusqlite::Error, context: impl Into<String>) -> Self {
41         Self::Sql {
42             inner: e,
43             context: context.into(),
44         }
45     }
48 impl From<rusqlite::Error> for Error {
49     fn from(e: rusqlite::Error) -> Self {
50         Self::sql(e, "<none>")
51     }
54 #[extend::ext(name=RusqliteResultExt)]
55 pub impl<T> Result<T, rusqlite::Error> {
56     // Convert an rusqlite::Error to our error type, with a context value
57     fn with_context(self, context: &str) -> Result<T, Error> {
58         self.map_err(|e| Error::sql(e, context))
59     }
62 /// The error type for all Suggest component operations. These errors are
63 /// exposed to your application, which should handle them as needed.
64 #[derive(Debug, thiserror::Error)]
65 #[non_exhaustive]
66 pub enum SuggestApiError {
67     #[error("Network error: {reason}")]
68     Network { reason: String },
69     // The server requested a backoff after too many requests
70     #[error("Backoff")]
71     Backoff { seconds: u64 },
72     // The application interrupted a request
73     #[error("Interrupted")]
74     Interrupted,
75     #[error("Other error: {reason}")]
76     Other { reason: String },
79 // Define how our internal errors are handled and converted to external errors
80 // See `support/error/README.md` for how this works, especially the warning about PII.
81 impl GetErrorHandling for Error {
82     type ExternalError = SuggestApiError;
84     fn get_error_handling(&self) -> ErrorHandling<Self::ExternalError> {
85         match self {
86             // Do nothing for interrupted errors, this is just normal operation.
87             Self::Interrupted(_) => ErrorHandling::convert(SuggestApiError::Interrupted),
88             // Network errors are expected to happen in practice.  Let's log, but not report them.
89             Self::RemoteSettings(RemoteSettingsError::RequestError(
90                 viaduct::Error::NetworkError(e),
91             )) => ErrorHandling::convert(SuggestApiError::Network {
92                 reason: e.to_string(),
93             })
94             .log_warning(),
95             // Backoff error shouldn't happen in practice, so let's report them for now.
96             // If these do happen in practice and we decide that there is a valid reason for them,
97             // then consider switching from reporting to Sentry to counting in Glean.
98             Self::RemoteSettings(RemoteSettingsError::BackoffError(seconds)) => {
99                 ErrorHandling::convert(SuggestApiError::Backoff { seconds: *seconds })
100                     .report_error("suggest-backoff")
101             }
102             _ => ErrorHandling::convert(SuggestApiError::Other {
103                 reason: self.to_string(),
104             })
105             .report_error("suggest-unexpected"),
106         }
107     }