diff options
| author | David Lönnhager <david.l@mullvad.net> | 2021-03-22 19:29:38 +0100 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2021-03-25 13:42:07 +0100 |
| commit | 53462809d67bc5073a8bd793b997f1a0b4fd8e2a (patch) | |
| tree | 5476f3a212e976d5eabfe918254adb2825113bc8 | |
| parent | 5466e50a2e8c1da9a47c47f0acc21f3d3ce84653 (diff) | |
| download | mullvadvpn-53462809d67bc5073a8bd793b997f1a0b4fd8e2a.tar.xz mullvadvpn-53462809d67bc5073a8bd793b997f1a0b4fd8e2a.zip | |
Implement Error trait for rotation interval error
| -rw-r--r-- | mullvad-daemon/src/management_interface.rs | 5 | ||||
| -rw-r--r-- | mullvad-types/src/wireguard.rs | 21 |
2 files changed, 23 insertions, 3 deletions
diff --git a/mullvad-daemon/src/management_interface.rs b/mullvad-daemon/src/management_interface.rs index a6762e867b..dff318cece 100644 --- a/mullvad-daemon/src/management_interface.rs +++ b/mullvad-daemon/src/management_interface.rs @@ -606,9 +606,8 @@ impl ManagementService for ManagementServiceImpl { let interval: RotationInterval = Duration::try_from(request.into_inner()) .map_err(|_| Status::invalid_argument("unexpected negative rotation interval"))? .try_into() - .map_err(|error| match error { - RotationIntervalError::TooSmall => Status::invalid_argument("interval too small"), - RotationIntervalError::TooLarge => Status::invalid_argument("interval too large"), + .map_err(|error: RotationIntervalError| { + Status::invalid_argument(error.display_chain()) })?; log::debug!("set_wireguard_rotation_interval({:?})", interval); diff --git a/mullvad-types/src/wireguard.rs b/mullvad-types/src/wireguard.rs index 94fc31f29e..cbe05363ec 100644 --- a/mullvad-types/src/wireguard.rs +++ b/mullvad-types/src/wireguard.rs @@ -38,6 +38,27 @@ pub enum RotationIntervalError { TooLarge, } +impl fmt::Display for RotationIntervalError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + use RotationIntervalError::*; + + match *self { + TooSmall => write!( + f, + "Rotation interval must be at least {} hours", + MIN_ROTATION_INTERVAL.as_secs() / 60 / 60 + ), + TooLarge => write!( + f, + "Rotation interval must be at most {} hours", + MAX_ROTATION_INTERVAL.as_secs() / 60 / 60 + ), + } + } +} + +impl std::error::Error for RotationIntervalError {} + #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] pub struct RotationInterval(Duration); |
