|
| 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 | +pub use error_support::error; |
| 6 | +use error_support::{ErrorHandling, GetErrorHandling}; |
| 7 | + |
| 8 | +pub type Result<T> = std::result::Result<T, Error>; |
| 9 | +pub type ApiResult<T> = std::result::Result<T, MerinoWorldCupApiError>; |
| 10 | + |
| 11 | +#[derive(Debug, thiserror::Error, uniffi::Error)] |
| 12 | +pub enum MerinoWorldCupApiError { |
| 13 | + /// A network-level failure. |
| 14 | + #[error("WorldCup network error: {reason}")] |
| 15 | + Network { reason: String }, |
| 16 | + |
| 17 | + /// Any other error, e.g. HTTP errors, validation errors. |
| 18 | + #[error("WorldCup error: code {code:?}, reason: {reason}")] |
| 19 | + Other { code: Option<u16>, reason: String }, |
| 20 | +} |
| 21 | + |
| 22 | +#[derive(Debug, thiserror::Error)] |
| 23 | +pub enum Error { |
| 24 | + /// Failed to parse a URL. |
| 25 | + #[error("URL parse error: {0}")] |
| 26 | + UrlParse(#[from] url::ParseError), |
| 27 | + |
| 28 | + /// Failed to send the HTTP request. |
| 29 | + #[error("Error sending request: {0}")] |
| 30 | + Request(#[from] viaduct::ViaductError), |
| 31 | + |
| 32 | + /// The server rejected the request due to malformed syntax (HTTP 400). |
| 33 | + #[error("Bad request ({code}): {message}")] |
| 34 | + BadRequest { code: u16, message: String }, |
| 35 | + |
| 36 | + /// The server rejected the request due to invalid input (HTTP 422). |
| 37 | + #[error("Validation error ({code}): {message}")] |
| 38 | + Validation { code: u16, message: String }, |
| 39 | + |
| 40 | + /// The server encountered an internal error (HTTP 5xx). |
| 41 | + #[error("Server error ({code}): {message}")] |
| 42 | + Server { code: u16, message: String }, |
| 43 | + |
| 44 | + /// An unexpected HTTP status code was received. |
| 45 | + #[error("Unexpected error ({code}): {message}")] |
| 46 | + Unexpected { code: u16, message: String }, |
| 47 | +} |
| 48 | + |
| 49 | +impl GetErrorHandling for Error { |
| 50 | + type ExternalError = MerinoWorldCupApiError; |
| 51 | + |
| 52 | + fn get_error_handling(&self) -> ErrorHandling<Self::ExternalError> { |
| 53 | + match self { |
| 54 | + Self::Request { .. } => ErrorHandling::convert(MerinoWorldCupApiError::Network { |
| 55 | + reason: self.to_string(), |
| 56 | + }) |
| 57 | + .log_warning(), |
| 58 | + |
| 59 | + Self::Validation { code, .. } |
| 60 | + | Self::Server { code, .. } |
| 61 | + | Self::Unexpected { code, .. } |
| 62 | + | Self::BadRequest { code, .. } => { |
| 63 | + ErrorHandling::convert(MerinoWorldCupApiError::Other { |
| 64 | + code: Some(*code), |
| 65 | + reason: self.to_string(), |
| 66 | + }) |
| 67 | + .report_error("merino-http-error") |
| 68 | + } |
| 69 | + |
| 70 | + Self::UrlParse(_) => ErrorHandling::convert(MerinoWorldCupApiError::Other { |
| 71 | + code: None, |
| 72 | + reason: self.to_string(), |
| 73 | + }) |
| 74 | + .report_error("merino-unexpected"), |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments