diff options
Diffstat (limited to 'mullvad-relay-selector/src/error.rs')
| -rw-r--r-- | mullvad-relay-selector/src/error.rs | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/mullvad-relay-selector/src/error.rs b/mullvad-relay-selector/src/error.rs new file mode 100644 index 0000000000..f988be1b89 --- /dev/null +++ b/mullvad-relay-selector/src/error.rs @@ -0,0 +1,66 @@ +//! Definition of relay selector errors +#![allow(dead_code)] + +use mullvad_types::{relay_constraints::MissingCustomBridgeSettings, relay_list::Relay}; + +use crate::{detailer, WireguardConfig}; + +#[derive(thiserror::Error, Debug)] +pub enum Error { + #[error("Failed to open relay cache file")] + OpenRelayCache(#[source] std::io::Error), + + #[error("Failed to write relay cache file to disk")] + WriteRelayCache(#[source] std::io::Error), + + #[error("No relays matching current constraints")] + NoRelay, + + #[error("No bridges matching current constraints")] + NoBridge, + + #[error("No obfuscators matching current constraints")] + NoObfuscator, + + #[error("No endpoint could be constructed due to {} for relay {:?}", .internal, .relay)] + NoEndpoint { + internal: detailer::Error, + relay: EndpointErrorDetails, + }, + + #[error("Failure in serialization of the relay list")] + Serialize(#[from] serde_json::Error), + + #[error("Invalid bridge settings")] + InvalidBridgeSettings(#[from] MissingCustomBridgeSettings), +} + +/// Special type which only shows up in [`Error`]. This error variant signals that no valid +/// endpoint could be constructed from the selected relay. +#[derive(Debug)] +pub enum EndpointErrorDetails { + /// No valid Wireguard endpoint could be constructed from this [`WireguardConfig`]. + /// + /// # Note + /// The inner value is boxed to not bloat the size of [`Error`] due to the size of [`WireguardConfig`]. + Wireguard(Box<WireguardConfig>), + /// No valid OpenVPN endpoint could be constructed from this [`Relay`] + /// + /// # Note + /// The inner value is boxed to not bloat the size of [`Error`] due to the size of [`Relay`]. + OpenVpn(Box<Relay>), +} + +impl EndpointErrorDetails { + /// Helper function for constructing an [`Error::NoEndpoint`] from `relay`. + /// Takes care of boxing the [`WireguardConfig`] for you! + pub(crate) fn from_wireguard(relay: WireguardConfig) -> Self { + EndpointErrorDetails::Wireguard(Box::new(relay)) + } + + /// Helper function for constructing an [`Error::NoEndpoint`] from `relay`. + /// Takes care of boxing the [`Relay`] for you! + pub(crate) fn from_openvpn(relay: Relay) -> Self { + EndpointErrorDetails::OpenVpn(Box::new(relay)) + } +} |
