relay/
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
5pub use error_support::error;
6use error_support::{ErrorHandling, GetErrorHandling};
7
8pub type Result<T> = std::result::Result<T, Error>;
9pub type ApiResult<T> = std::result::Result<T, RelayApiError>;
10
11#[derive(Debug, thiserror::Error, uniffi::Error)]
12pub enum RelayApiError {
13    #[error("Relay network error: {reason}")]
14    Network { reason: String },
15
16    #[error("Relay API error: {detail}")]
17    RelayApi { detail: String },
18
19    #[error("Relay unexpected error: {reason}")]
20    Other { reason: String },
21}
22
23#[derive(Debug, thiserror::Error)]
24pub enum Error {
25    #[error("Relay API error: {0}")]
26    RelayApi(String),
27
28    #[error("JSON parsing error: {0}")]
29    Json(#[from] serde_json::Error),
30    #[error("HTTP request error: {0}")]
31    Viaduct(#[from] viaduct::Error),
32    #[error("URL parsing error: {0}")]
33    UrlParse(#[from] url::ParseError),
34}
35
36impl GetErrorHandling for Error {
37    type ExternalError = RelayApiError;
38
39    fn get_error_handling(&self) -> ErrorHandling<Self::ExternalError> {
40        match self {
41            Error::Viaduct(viaduct::Error::NetworkError(e)) => {
42                ErrorHandling::convert(RelayApiError::Network {
43                    reason: e.to_string(),
44                })
45                .log_warning()
46            }
47            Error::RelayApi(detail) => ErrorHandling::convert(RelayApiError::RelayApi {
48                detail: detail.clone(),
49            })
50            .report_error("relay-api-error"),
51            _ => ErrorHandling::convert(RelayApiError::Other {
52                reason: self.to_string(),
53            })
54            .report_error("relay-unexpected-error"),
55        }
56    }
57}