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/.
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
12 #[derive(Debug, thiserror::Error)]
14 #[error("Error opening database: {0}")]
15 OpenDatabase(#[from] sql_support::open_database::Error),
17 #[error("Error executing SQL: {inner} (context: {context})")]
19 inner: rusqlite::Error,
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),
40 fn sql(e: rusqlite::Error, context: impl Into<String>) -> Self {
43 context: context.into(),
48 impl From<rusqlite::Error> for Error {
49 fn from(e: rusqlite::Error) -> Self {
50 Self::sql(e, "<none>")
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))
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)]
66 pub enum SuggestApiError {
67 #[error("Network error: {reason}")]
68 Network { reason: String },
69 // The server requested a backoff after too many requests
71 Backoff { seconds: u64 },
72 // The application interrupted a request
73 #[error("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> {
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(),
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")
102 _ => ErrorHandling::convert(SuggestApiError::Other {
103 reason: self.to_string(),
105 .report_error("suggest-unexpected"),