]> git.ipfire.org Git - thirdparty/wireguard-apple.git/commitdiff
Error handling: Introduce a WireGuardAppError protocol to manage errors
authorRoopesh Chander <roop@roopc.net>
Thu, 6 Dec 2018 10:43:48 +0000 (16:13 +0530)
committerRoopesh Chander <roop@roopc.net>
Fri, 7 Dec 2018 07:06:19 +0000 (12:36 +0530)
The alert strings shall be located next to where the errors are declared.

Signed-off-by: Roopesh Chander <roop@roopc.net>
WireGuard/WireGuard.xcodeproj/project.pbxproj
WireGuard/WireGuard/UI/iOS/ErrorPresenter.swift
WireGuard/WireGuard/VPN/TunnelsManager.swift
WireGuard/WireGuard/WireGuardAppError.swift [new file with mode: 0644]

index a7db4ab62844908e6d6c731a253b7ac366003c7f..f5bdbe48ccc96efc05ee3b982a4f7e3dca53e16a 100644 (file)
@@ -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 */; };
                6F5D0C1F218352EF000F85AD /* WireGuardNetworkExtension.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = WireGuardNetworkExtension.entitlements; sourceTree = "<group>"; };
                6F5D0C3421839E37000F85AD /* WireGuardNetworkExtension-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "WireGuardNetworkExtension-Bridging-Header.h"; sourceTree = "<group>"; };
                6F5D0C472183C6A3000F85AD /* PacketTunnelSettingsGenerator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PacketTunnelSettingsGenerator.swift; sourceTree = "<group>"; };
+               6F61F1E821B932F700483816 /* WireGuardAppError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WireGuardAppError.swift; sourceTree = "<group>"; };
                6F628C3C217F09E9003482A3 /* TunnelViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TunnelViewModel.swift; sourceTree = "<group>"; };
                6F628C3E217F3413003482A3 /* DNSServer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DNSServer.swift; sourceTree = "<group>"; };
                6F628C40217F47DB003482A3 /* TunnelDetailTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TunnelDetailTableViewController.swift; sourceTree = "<group>"; };
                                6F7774DD217181B1006A79B3 /* UI */,
                                6F7774ED21722D0C006A79B3 /* VPN */,
                                6FDEF7E72186320E00D8FBF6 /* ZipArchive */,
+                               6F61F1E821B932F700483816 /* WireGuardAppError.swift */,
                                6FF4AC482120B9E0002C96EB /* WireGuard.entitlements */,
                                6FF4AC1E211EC472002C96EB /* Assets.xcassets */,
                                6FF4AC20211EC472002C96EB /* LaunchScreen.storyboard */,
                                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 */,
index 35bbdec87a10b956dae696915c7a0b20325e71aa..b085b97645924b8271a97332e56251173e604e17 100644 (file)
@@ -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 }
index 77eb8e5caf6be6c43bfe6d23616f0337e0d996a7..03d1a45694e07787aa0ed7c0efbff2bdf68e5c4c 100644 (file)
@@ -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 (file)
index 0000000..5289633
--- /dev/null
@@ -0,0 +1,6 @@
+// SPDX-License-Identifier: MIT
+// Copyright © 2018 WireGuard LLC. All Rights Reserved.
+
+protocol WireGuardAppError: Error {
+    func alertText() -> (/* title */ String, /* message */ String)
+}