Tunnel errors: Consolidate tunnel activation errors into ErrorPresenter

This commit is contained in:
Roopesh Chander 2018-11-01 16:58:33 +05:30
parent e222c8391e
commit 479f04a579
3 changed files with 36 additions and 16 deletions

View File

@ -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

View File

@ -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
}
}
}

View File

@ -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
}
}
}