relevancy/
error.rs

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
6use error_support::{ErrorHandling, GetErrorHandling};
7
8/// Errors we return via the public interface.
9#[derive(Debug, thiserror::Error, uniffi::Error)]
10pub enum RelevancyApiError {
11    #[error("Unexpected Error: {reason}")]
12    Unexpected { reason: String },
13}
14
15/// Errors we use internally
16#[derive(Debug, thiserror::Error)]
17pub enum Error {
18    #[error("Error opening database: {0}")]
19    OpenDatabase(#[from] sql_support::open_database::Error),
20
21    #[error("Sql error: {0}")]
22    SqlError(#[from] rusqlite::Error),
23
24    #[error("Error fetching interest data")]
25    FetchInterestDataError,
26
27    #[error("Interrupted")]
28    Interrupted(#[from] interrupt_support::Interrupted),
29
30    #[error("Invalid interest code: {0}")]
31    InvalidInterestCode(u32),
32
33    #[error("Remote Setting Error: {0}")]
34    RemoteSettingsError(#[from] remote_settings::RemoteSettingsError),
35
36    #[error("Error parsing {type_name}: {error} ({path})")]
37    RemoteSettingsParseError {
38        type_name: String,
39        path: String,
40        error: serde_json::Error,
41    },
42
43    #[error("Base64 Decode Error: {0}")]
44    Base64DecodeError(String),
45
46    #[error("Error retrieving bandit data for bandit {bandit} and arm {arm}")]
47    BanditNotFound { bandit: String, arm: String },
48}
49
50/// Result enum for the public API
51pub type ApiResult<T> = std::result::Result<T, RelevancyApiError>;
52
53/// Result enum for internal functions
54pub type Result<T> = std::result::Result<T, Error>;
55
56// 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 {
59    type ExternalError = RelevancyApiError;
60
61    fn get_error_handling(&self) -> ErrorHandling<Self::ExternalError> {
62        match self {
63            Self::RemoteSettingsParseError { .. } => {
64                ErrorHandling::convert(RelevancyApiError::Unexpected {
65                    reason: self.to_string(),
66                })
67                .report_error("relevancy-remote-settings-parse-error")
68            }
69
70            _ => ErrorHandling::convert(RelevancyApiError::Unexpected {
71                reason: self.to_string(),
72            }),
73        }
74    }
75}