context_id/
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
5use error_support::{ErrorHandling, GetErrorHandling};
6
7pub type Result<T> = std::result::Result<T, Error>;
8pub type ApiResult<T> = std::result::Result<T, ApiError>;
9
10#[derive(Debug, thiserror::Error, uniffi::Error)]
11pub enum ApiError {
12    #[error("Something unexpected occurred.")]
13    Other { reason: String },
14}
15
16#[derive(Debug, thiserror::Error)]
17pub enum Error {
18    #[error("Timestamp was invalid")]
19    InvalidTimestamp { timestamp: i64 },
20
21    #[error("URL parse error: {0}")]
22    UrlParseError(#[from] url::ParseError),
23
24    #[error("Viaduct error: {0}")]
25    ViaductError(#[from] viaduct::Error),
26
27    #[error("UniFFI callback error: {0}")]
28    UniFFICallbackError(#[from] uniffi::UnexpectedUniFFICallbackError),
29}
30
31// Define how our internal errors are handled and converted to external errors.
32impl GetErrorHandling for Error {
33    type ExternalError = ApiError;
34
35    fn get_error_handling(&self) -> ErrorHandling<Self::ExternalError> {
36        ErrorHandling::convert(ApiError::Other {
37            reason: self.to_string(),
38        })
39    }
40}