diff options
author | Roopesh Chander <roop@roopc.net> | 2018-12-06 16:13:48 +0530 |
---|---|---|
committer | Roopesh Chander <roop@roopc.net> | 2018-12-07 12:36:19 +0530 |
commit | c9267ba634f5699318502035c03fa281361cc581 (patch) | |
tree | 65cb7106e0fcd3915efd5a2a1135da1b0a05fda3 /WireGuard/WireGuard | |
parent | Error handling: Cleanup Tunnels Manager errors (diff) | |
download | wireguard-apple-c9267ba634f5699318502035c03fa281361cc581.tar.xz wireguard-apple-c9267ba634f5699318502035c03fa281361cc581.zip |
Error handling: Introduce a WireGuardAppError protocol to manage errors
The alert strings shall be located next to where the errors are declared.
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to 'WireGuard/WireGuard')
-rw-r--r-- | WireGuard/WireGuard/UI/iOS/ErrorPresenter.swift | 30 | ||||
-rw-r--r-- | WireGuard/WireGuard/VPN/TunnelsManager.swift | 28 | ||||
-rw-r--r-- | WireGuard/WireGuard/WireGuardAppError.swift | 6 |
3 files changed, 35 insertions, 29 deletions
diff --git a/WireGuard/WireGuard/UI/iOS/ErrorPresenter.swift b/WireGuard/WireGuard/UI/iOS/ErrorPresenter.swift index 35bbdec..b085b97 100644 --- a/WireGuard/WireGuard/UI/iOS/ErrorPresenter.swift +++ b/WireGuard/WireGuard/UI/iOS/ErrorPresenter.swift @@ -7,8 +7,8 @@ import os.log class ErrorPresenter { static func errorMessage(for error: Error) -> (String, String) { - if let tunnelsManagerError = error as? TunnelsManagerError { - return errorMessage(forTunnelsManagerError: tunnelsManagerError) + if let error = error as? WireGuardAppError { + return error.alertText() } switch (error) { @@ -32,32 +32,6 @@ class ErrorPresenter { } } - private static func errorMessage(forTunnelsManagerError error: TunnelsManagerError) -> (String, String) { - switch (error) { - // Tunnels list management - case TunnelsManagerError.tunnelNameEmpty: - return ("No name provided", "Can't create tunnel with an empty name") - case TunnelsManagerError.tunnelAlreadyExistsWithThatName: - return ("Name already exists", "A tunnel with that name already exists") - case TunnelsManagerError.vpnSystemErrorOnListingTunnels: - return ("Unable to list tunnels", "Internal error") - case TunnelsManagerError.vpnSystemErrorOnAddTunnel: - return ("Unable to create tunnel", "Internal error") - case TunnelsManagerError.vpnSystemErrorOnModifyTunnel: - return ("Unable to modify tunnel", "Internal error") - case TunnelsManagerError.vpnSystemErrorOnRemoveTunnel: - return ("Unable to remove tunnel", "Internal error") - - // Tunnel activation - case TunnelsManagerError.tunnelActivationAttemptFailed: - return ("Activation failure", "The tunnel could not be activated due to an internal error") - case TunnelsManagerError.tunnelActivationFailedInternalError: - return ("Activation failure", "The tunnel could not be activated due to an internal error") - case TunnelsManagerError.tunnelActivationFailedNoInternetConnection: - return ("Activation failure", "No internet connection") - } - } - static func showErrorAlert(error: Error, from sourceVC: UIViewController?, onDismissal: (() -> Void)? = nil, onPresented: (() -> Void)? = nil) { guard let sourceVC = sourceVC else { return } diff --git a/WireGuard/WireGuard/VPN/TunnelsManager.swift b/WireGuard/WireGuard/VPN/TunnelsManager.swift index 77eb8e5..03d1a45 100644 --- a/WireGuard/WireGuard/VPN/TunnelsManager.swift +++ b/WireGuard/WireGuard/VPN/TunnelsManager.swift @@ -16,7 +16,8 @@ protocol TunnelsManagerActivationDelegate: class { func tunnelActivationFailed(tunnel: TunnelContainer, error: TunnelsManagerError) } -enum TunnelsManagerError: Error { +enum TunnelsManagerError: WireGuardAppError { + // Tunnels list management case tunnelNameEmpty case tunnelAlreadyExistsWithThatName case vpnSystemErrorOnListingTunnels @@ -24,9 +25,34 @@ enum TunnelsManagerError: Error { case vpnSystemErrorOnModifyTunnel case vpnSystemErrorOnRemoveTunnel + // Tunnel activation case tunnelActivationAttemptFailed // startTunnel() throwed case tunnelActivationFailedInternalError // startTunnel() succeeded, but activation failed case tunnelActivationFailedNoInternetConnection // startTunnel() succeeded, but activation failed since no internet + + func alertText() -> (String, String) { + switch (self) { + case .tunnelNameEmpty: + return ("No name provided", "Can't create tunnel with an empty name") + case .tunnelAlreadyExistsWithThatName: + return ("Name already exists", "A tunnel with that name already exists") + case .vpnSystemErrorOnListingTunnels: + return ("Unable to list tunnels", "Internal error") + case .vpnSystemErrorOnAddTunnel: + return ("Unable to create tunnel", "Internal error") + case .vpnSystemErrorOnModifyTunnel: + return ("Unable to modify tunnel", "Internal error") + case .vpnSystemErrorOnRemoveTunnel: + return ("Unable to remove tunnel", "Internal error") + + case .tunnelActivationAttemptFailed: + return ("Activation failure", "The tunnel could not be activated due to an internal error") + case .tunnelActivationFailedInternalError: + return ("Activation failure", "The tunnel could not be activated due to an internal error") + case .tunnelActivationFailedNoInternetConnection: + return ("Activation failure", "No internet connection") + } + } } enum TunnelsManagerResult<T> { diff --git a/WireGuard/WireGuard/WireGuardAppError.swift b/WireGuard/WireGuard/WireGuardAppError.swift new file mode 100644 index 0000000..5289633 --- /dev/null +++ b/WireGuard/WireGuard/WireGuardAppError.swift @@ -0,0 +1,6 @@ +// SPDX-License-Identifier: MIT +// Copyright © 2018 WireGuard LLC. All Rights Reserved. + +protocol WireGuardAppError: Error { + func alertText() -> (/* title */ String, /* message */ String) +} |