From c9267ba634f5699318502035c03fa281361cc581 Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Thu, 6 Dec 2018 16:13:48 +0530 Subject: [PATCH] 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 --- WireGuard/WireGuard.xcodeproj/project.pbxproj | 4 +++ .../WireGuard/UI/iOS/ErrorPresenter.swift | 30 ++----------------- WireGuard/WireGuard/VPN/TunnelsManager.swift | 28 ++++++++++++++++- WireGuard/WireGuard/WireGuardAppError.swift | 6 ++++ 4 files changed, 39 insertions(+), 29 deletions(-) create mode 100644 WireGuard/WireGuard/WireGuardAppError.swift diff --git a/WireGuard/WireGuard.xcodeproj/project.pbxproj b/WireGuard/WireGuard.xcodeproj/project.pbxproj index a7db4ab..f5bdbe4 100644 --- a/WireGuard/WireGuard.xcodeproj/project.pbxproj +++ b/WireGuard/WireGuard.xcodeproj/project.pbxproj @@ -13,6 +13,7 @@ 6F5A2B4821AFF49A0081EDD8 /* FileManager+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F5A2B4421AFDE020081EDD8 /* FileManager+Extension.swift */; }; 6F5D0C1D218352EF000F85AD /* PacketTunnelProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F5D0C1C218352EF000F85AD /* PacketTunnelProvider.swift */; }; 6F5D0C22218352EF000F85AD /* WireGuardNetworkExtension.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = 6F5D0C1A218352EF000F85AD /* WireGuardNetworkExtension.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; }; + 6F61F1E921B932F700483816 /* WireGuardAppError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F61F1E821B932F700483816 /* WireGuardAppError.swift */; }; 6F628C3D217F09E9003482A3 /* TunnelViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F628C3C217F09E9003482A3 /* TunnelViewModel.swift */; }; 6F628C3F217F3413003482A3 /* DNSServer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F628C3E217F3413003482A3 /* DNSServer.swift */; }; 6F628C41217F47DB003482A3 /* TunnelDetailTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F628C40217F47DB003482A3 /* TunnelDetailTableViewController.swift */; }; @@ -99,6 +100,7 @@ 6F5D0C1F218352EF000F85AD /* WireGuardNetworkExtension.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = WireGuardNetworkExtension.entitlements; sourceTree = ""; }; 6F5D0C3421839E37000F85AD /* WireGuardNetworkExtension-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "WireGuardNetworkExtension-Bridging-Header.h"; sourceTree = ""; }; 6F5D0C472183C6A3000F85AD /* PacketTunnelSettingsGenerator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PacketTunnelSettingsGenerator.swift; sourceTree = ""; }; + 6F61F1E821B932F700483816 /* WireGuardAppError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WireGuardAppError.swift; sourceTree = ""; }; 6F628C3C217F09E9003482A3 /* TunnelViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TunnelViewModel.swift; sourceTree = ""; }; 6F628C3E217F3413003482A3 /* DNSServer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DNSServer.swift; sourceTree = ""; }; 6F628C40217F47DB003482A3 /* TunnelDetailTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TunnelDetailTableViewController.swift; sourceTree = ""; }; @@ -335,6 +337,7 @@ 6F7774DD217181B1006A79B3 /* UI */, 6F7774ED21722D0C006A79B3 /* VPN */, 6FDEF7E72186320E00D8FBF6 /* ZipArchive */, + 6F61F1E821B932F700483816 /* WireGuardAppError.swift */, 6FF4AC482120B9E0002C96EB /* WireGuard.entitlements */, 6FF4AC1E211EC472002C96EB /* Assets.xcassets */, 6FF4AC20211EC472002C96EB /* LaunchScreen.storyboard */, @@ -559,6 +562,7 @@ 6F0068572191AFD200419BE9 /* ScrollableLabel.swift in Sources */, 6FDEF7E62185EFB200D8FBF6 /* QRScanViewController.swift in Sources */, 6FFA5D952194454A0001E2F7 /* NETunnelProviderProtocol+Extension.swift in Sources */, + 6F61F1E921B932F700483816 /* WireGuardAppError.swift in Sources */, 6F6899A62180447E0012E523 /* x25519.c in Sources */, 6F7774E2217181B1006A79B3 /* AppDelegate.swift in Sources */, 6FDEF80021863C0100D8FBF6 /* ioapi.c in Sources */, 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 { 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) +}