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*/
56use error_support::{ErrorHandling, GetErrorHandling};
78/// Errors we return via the public interface.
9#[derive(Debug, thiserror::Error, uniffi::Error)]
10pub enum RelevancyApiError {
11#[error("Unexpected Error: {reason}")]
12Unexpected { reason: String },
13}
1415/// Errors we use internally
16#[derive(Debug, thiserror::Error)]
17pub enum Error {
18#[error("Error opening database: {0}")]
19OpenDatabase(#[from] sql_support::open_database::Error),
2021#[error("Sql error: {0}")]
22SqlError(#[from] rusqlite::Error),
2324#[error("Error fetching interest data")]
25FetchInterestDataError,
2627#[error("Interrupted")]
28Interrupted(#[from] interrupt_support::Interrupted),
2930#[error("Invalid interest code: {0}")]
31InvalidInterestCode(u32),
3233#[error("Remote Setting Error: {0}")]
34RemoteSettingsError(#[from] remote_settings::RemoteSettingsError),
3536#[error("Error parsing {type_name}: {error} ({path})")]
37RemoteSettingsParseError {
38 type_name: String,
39 path: String,
40 error: serde_json::Error,
41 },
4243#[error("Base64 Decode Error: {0}")]
44Base64DecodeError(String),
4546#[error("Error retrieving bandit data for bandit {bandit} and arm {arm}")]
47BanditNotFound { bandit: String, arm: String },
48}
4950/// Result enum for the public API
51pub type ApiResult<T> = std::result::Result<T, RelevancyApiError>;
5253/// Result enum for internal functions
54pub type Result<T> = std::result::Result<T, Error>;
5556// Define how our internal errors are handled and converted to external errors
57// See `support/error/README.md` for how this works, especially the warning about PII.
58impl GetErrorHandling for Error {
59type ExternalError = RelevancyApiError;
6061fn get_error_handling(&self) -> ErrorHandling<Self::ExternalError> {
62match self {
63Self::RemoteSettingsParseError { .. } => {
64 ErrorHandling::convert(RelevancyApiError::Unexpected {
65 reason: self.to_string(),
66 })
67 .report_error("relevancy-remote-settings-parse-error")
68 }
6970_ => ErrorHandling::convert(RelevancyApiError::Unexpected {
71 reason: self.to_string(),
72 }),
73 }
74 }
75}