From 3611f8cf5afe709a9d84a4edd68666480ed04223 Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Thu, 1 Nov 2018 16:58:33 +0530 Subject: [PATCH] Tunnel errors: Consolidate tunnel activation errors into ErrorPresenter Signed-off-by: Roopesh Chander --- .../WireGuard/UI/iOS/ErrorPresenter.swift | 28 +++++++++++++++++++ .../iOS/TunnelDetailTableViewController.swift | 12 +++----- .../iOS/TunnelsListTableViewController.swift | 12 +++----- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/WireGuard/WireGuard/UI/iOS/ErrorPresenter.swift b/WireGuard/WireGuard/UI/iOS/ErrorPresenter.swift index 8cdb921..e7733b3 100644 --- a/WireGuard/WireGuard/UI/iOS/ErrorPresenter.swift +++ b/WireGuard/WireGuard/UI/iOS/ErrorPresenter.swift @@ -7,6 +7,8 @@ import os.log class ErrorPresenter { static func errorMessage(for error: Error) -> (String, String)? { switch (error) { + + // TunnelManagementError case TunnelManagementError.tunnelAlreadyExistsWithThatName: return ("Name already in use", "A tunnel with that name already exists. Please pick a different name.") case TunnelManagementError.vpnSystemErrorOnAddTunnel: @@ -15,6 +17,32 @@ class ErrorPresenter { return ("Could not modify tunnel", "Internal error") case TunnelManagementError.vpnSystemErrorOnRemoveTunnel: return ("Could not remove tunnel", "Internal error") + + // TunnelActivationError + case TunnelActivationError.noEndpoint: + return ("Endpoint missing", "There must be at least one peer with an endpoint") + case TunnelActivationError.dnsResolutionFailed: + return ("DNS Failure", "One or more endpoint domains could not be resolved") + case TunnelActivationError.tunnelActivationFailed: + return ("Activation failed", "The tunnel could not be activated because of an internal error") + case TunnelActivationError.attemptingActivationWhenAnotherTunnelIsBusy(let otherTunnelStatus): + let statusString: String = { + switch (otherTunnelStatus) { + case .active: fallthrough + case .reasserting: fallthrough + case .restarting: + return "active" + case .activating: fallthrough + case .resolvingEndpointDomains: + return "being activated" + case .deactivating: + return "being deactivated" + case .inactive: + fatalError() + } + }() + return ("Activation failed", "Another tunnel is currently \(statusString). Only one tunnel can be in operation at a time.") + default: os_log("ErrorPresenter: Error not presented: %{public}@", log: OSLog.default, type: .error, "\(error)") return nil diff --git a/WireGuard/WireGuard/UI/iOS/TunnelDetailTableViewController.swift b/WireGuard/WireGuard/UI/iOS/TunnelDetailTableViewController.swift index 14d715a..dc63258 100644 --- a/WireGuard/WireGuard/UI/iOS/TunnelDetailTableViewController.swift +++ b/WireGuard/WireGuard/UI/iOS/TunnelDetailTableViewController.swift @@ -149,15 +149,11 @@ extension TunnelDetailTableViewController { cell.onSwitchToggled = { [weak self] isOn in guard let s = self else { return } if (isOn) { - s.tunnelsManager.startActivation(of: s.tunnel) { [weak self] error in + s.tunnelsManager.startActivation(of: s.tunnel) { [weak s] error in if let error = error { - switch (error) { - case TunnelActivationError.noEndpoint: - self?.showErrorAlert(title: "Endpoint missing", message: "There must be at least one peer with an endpoint") - case TunnelActivationError.dnsResolutionFailed: - self?.showErrorAlert(title: "DNS Failure", message: "One or more endpoint domains could not be resolved") - default: - self?.showErrorAlert(title: "Internal error", message: "The tunnel could not be activated") + ErrorPresenter.showErrorAlert(error: error, from: s) + DispatchQueue.main.async { + cell.statusSwitch.isOn = false } } } diff --git a/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift b/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift index 3ef672a..66b404c 100644 --- a/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift +++ b/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift @@ -245,15 +245,11 @@ extension TunnelsListTableViewController { cell.onSwitchToggled = { [weak self] isOn in guard let s = self, let tunnelsManager = s.tunnelsManager else { return } if (isOn) { - tunnelsManager.startActivation(of: tunnel) { error in + tunnelsManager.startActivation(of: tunnel) { [weak s] error in if let error = error { - switch (error) { - case TunnelActivationError.noEndpoint: - self?.showErrorAlert(title: "Endpoint missing", message: "There must be at least one peer with an endpoint") - case TunnelActivationError.dnsResolutionFailed: - self?.showErrorAlert(title: "DNS Failure", message: "One or more endpoint domains could not be resolved") - default: - self?.showErrorAlert(title: "Internal error", message: "The tunnel could not be activated") + ErrorPresenter.showErrorAlert(error: error, from: s) + DispatchQueue.main.async { + cell.statusSwitch.isOn = false } } }