From 8d58a79272bf33a977f213f13d286875c295c832 Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Thu, 6 Dec 2018 16:30:11 +0530 Subject: [PATCH] Error handling: Introduce a WireGuardResult type to handle errors in callbacks across the app --- WireGuard/WireGuard.xcodeproj/project.pbxproj | 4 +++ WireGuard/WireGuard/VPN/TunnelsManager.swift | 30 ++----------------- WireGuard/WireGuard/WireGuardResult.swift | 28 +++++++++++++++++ 3 files changed, 34 insertions(+), 28 deletions(-) create mode 100644 WireGuard/WireGuard/WireGuardResult.swift diff --git a/WireGuard/WireGuard.xcodeproj/project.pbxproj b/WireGuard/WireGuard.xcodeproj/project.pbxproj index f5bdbe4..dc22757 100644 --- a/WireGuard/WireGuard.xcodeproj/project.pbxproj +++ b/WireGuard/WireGuard.xcodeproj/project.pbxproj @@ -14,6 +14,7 @@ 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 */; }; + 6F61F1EB21B937EF00483816 /* WireGuardResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F61F1EA21B937EF00483816 /* WireGuardResult.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 */; }; @@ -101,6 +102,7 @@ 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 = ""; }; + 6F61F1EA21B937EF00483816 /* WireGuardResult.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WireGuardResult.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 = ""; }; @@ -338,6 +340,7 @@ 6F7774ED21722D0C006A79B3 /* VPN */, 6FDEF7E72186320E00D8FBF6 /* ZipArchive */, 6F61F1E821B932F700483816 /* WireGuardAppError.swift */, + 6F61F1EA21B937EF00483816 /* WireGuardResult.swift */, 6FF4AC482120B9E0002C96EB /* WireGuard.entitlements */, 6FF4AC1E211EC472002C96EB /* Assets.xcassets */, 6FF4AC20211EC472002C96EB /* LaunchScreen.storyboard */, @@ -578,6 +581,7 @@ 6FDEF7FB21863B6100D8FBF6 /* unzip.c in Sources */, 6F6899A8218044FC0012E523 /* Curve25519.swift in Sources */, 6F628C41217F47DB003482A3 /* TunnelDetailTableViewController.swift in Sources */, + 6F61F1EB21B937EF00483816 /* WireGuardResult.swift in Sources */, 6F7774F321774263006A79B3 /* TunnelEditTableViewController.swift in Sources */, 6FDEF802218646BA00D8FBF6 /* ZipArchive.swift in Sources */, 6FDEF806218725D200D8FBF6 /* SettingsTableViewController.swift in Sources */, diff --git a/WireGuard/WireGuard/VPN/TunnelsManager.swift b/WireGuard/WireGuard/VPN/TunnelsManager.swift index 03d1a45..4aada0c 100644 --- a/WireGuard/WireGuard/VPN/TunnelsManager.swift +++ b/WireGuard/WireGuard/VPN/TunnelsManager.swift @@ -55,32 +55,6 @@ enum TunnelsManagerError: WireGuardAppError { } } -enum TunnelsManagerResult { - case success(T) - case failure(TunnelsManagerError) - - var value: T? { - switch (self) { - case .success(let v): return v - case .failure(_): return nil - } - } - - var error: TunnelsManagerError? { - switch (self) { - case .success(_): return nil - case .failure(let e): return e - } - } - - var isSuccess: Bool { - switch (self) { - case .success(_): return true - case .failure(_): return false - } - } -} - class TunnelsManager { private var tunnels: [TunnelContainer] @@ -95,7 +69,7 @@ class TunnelsManager { self.tunnels = tunnelProviders.map { TunnelContainer(tunnel: $0) }.sorted { $0.name < $1.name } } - static func create(completionHandler: @escaping (TunnelsManagerResult) -> Void) { + static func create(completionHandler: @escaping (WireGuardResult) -> Void) { #if targetEnvironment(simulator) // NETunnelProviderManager APIs don't work on the simulator completionHandler(.success(TunnelsManager(tunnelProviders: []))) @@ -113,7 +87,7 @@ class TunnelsManager { func add(tunnelConfiguration: TunnelConfiguration, activateOnDemandSetting: ActivateOnDemandSetting = ActivateOnDemandSetting.defaultSetting, - completionHandler: @escaping (TunnelsManagerResult) -> Void) { + completionHandler: @escaping (WireGuardResult) -> Void) { let tunnelName = tunnelConfiguration.interface.name if tunnelName.isEmpty { completionHandler(.failure(TunnelsManagerError.tunnelNameEmpty)) diff --git a/WireGuard/WireGuard/WireGuardResult.swift b/WireGuard/WireGuard/WireGuardResult.swift new file mode 100644 index 0000000..93fc4c2 --- /dev/null +++ b/WireGuard/WireGuard/WireGuardResult.swift @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: MIT +// Copyright © 2018 WireGuard LLC. All Rights Reserved. + +enum WireGuardResult { + case success(T) + case failure(WireGuardAppError) + + var value: T? { + switch (self) { + case .success(let v): return v + case .failure(_): return nil + } + } + + var error: WireGuardAppError? { + switch (self) { + case .success(_): return nil + case .failure(let e): return e + } + } + + var isSuccess: Bool { + switch (self) { + case .success(_): return true + case .failure(_): return false + } + } +}