aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard/UI
diff options
context:
space:
mode:
authorRoopesh Chander <roop@roopc.net>2019-01-22 03:22:01 +0530
committerRoopesh Chander <roop@roopc.net>2019-01-22 03:26:01 +0530
commit6509009afc8c6e57cfa251050948521a6bc3640b (patch)
treebd25d4b4060b6fc128ca81bf4d357e8d9afa1693 /WireGuard/WireGuard/UI
parentmacOS: Observe private key changes for new tunnels too (diff)
downloadwireguard-apple-6509009afc8c6e57cfa251050948521a6bc3640b.tar.xz
wireguard-apple-6509009afc8c6e57cfa251050948521a6bc3640b.zip
macOS: Tunnel edit: Clean up error handling when saving
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to 'WireGuard/WireGuard/UI')
-rw-r--r--WireGuard/WireGuard/UI/macOS/ViewController/TunnelEditViewController.swift65
1 files changed, 30 insertions, 35 deletions
diff --git a/WireGuard/WireGuard/UI/macOS/ViewController/TunnelEditViewController.swift b/WireGuard/WireGuard/UI/macOS/ViewController/TunnelEditViewController.swift
index a5547a1e..78c2bcc2 100644
--- a/WireGuard/WireGuard/UI/macOS/ViewController/TunnelEditViewController.swift
+++ b/WireGuard/WireGuard/UI/macOS/ViewController/TunnelEditViewController.swift
@@ -186,48 +186,43 @@ class TunnelEditViewController: NSViewController {
} else {
onDemandSetting = ActivateOnDemandSetting(isActivateOnDemandEnabled: true, activateOnDemandOption: onDemandOption)
}
+
+ let isTunnelModifiedWithoutChangingName = (tunnel != nil && tunnel!.name == name)
+ guard isTunnelModifiedWithoutChangingName || tunnelsManager.tunnel(named: name) == nil else {
+ ErrorPresenter.showErrorAlert(title: tr(format: "macAlertDuplicateName (%@)", name), message: "", from: self)
+ return
+ }
+
+ let tunnelConfiguration: TunnelConfiguration
+ do {
+ tunnelConfiguration = try TunnelConfiguration(fromWgQuickConfig: textView.string, called: nameRow.value)
+ } catch let error as WireGuardAppError {
+ ErrorPresenter.showErrorAlert(error: error, from: self)
+ return
+ } catch {
+ fatalError()
+ }
+
if let tunnel = tunnel {
// We're modifying an existing tunnel
- if name != tunnel.name && tunnelsManager.tunnel(named: name) != nil {
- ErrorPresenter.showErrorAlert(title: tr(format: "macAlertDuplicateName (%@)", name), message: "", from: self)
- return
- }
- do {
- let tunnelConfiguration = try TunnelConfiguration(fromWgQuickConfig: textView.string, called: nameRow.value)
- tunnelsManager.modify(tunnel: tunnel, tunnelConfiguration: tunnelConfiguration, activateOnDemandSetting: onDemandSetting) { [weak self] error in
- if let error = error {
- ErrorPresenter.showErrorAlert(error: error, from: self)
- return
- }
- self?.dismiss(self)
- self?.delegate?.tunnelSaved(tunnel: tunnel)
+ tunnelsManager.modify(tunnel: tunnel, tunnelConfiguration: tunnelConfiguration, activateOnDemandSetting: onDemandSetting) { [weak self] error in
+ if let error = error {
+ ErrorPresenter.showErrorAlert(error: error, from: self)
+ return
}
- } catch let error as WireGuardAppError {
- ErrorPresenter.showErrorAlert(error: error, from: self)
- } catch {
- fatalError()
+ self?.dismiss(self)
+ self?.delegate?.tunnelSaved(tunnel: tunnel)
}
} else {
// We're creating a new tunnel
- if tunnelsManager.tunnel(named: name) != nil {
- ErrorPresenter.showErrorAlert(title: tr(format: "macAlertDuplicateName (%@)", name), message: "", from: self)
- return
- }
- do {
- let tunnelConfiguration = try TunnelConfiguration(fromWgQuickConfig: textView.string, called: nameRow.value)
- tunnelsManager.add(tunnelConfiguration: tunnelConfiguration, activateOnDemandSetting: onDemandSetting) { [weak self] result in
- if let error = result.error {
- ErrorPresenter.showErrorAlert(error: error, from: self)
- } else {
- let tunnel: TunnelContainer = result.value!
- self?.dismiss(self)
- self?.delegate?.tunnelSaved(tunnel: tunnel)
- }
+ tunnelsManager.add(tunnelConfiguration: tunnelConfiguration, activateOnDemandSetting: onDemandSetting) { [weak self] result in
+ if let error = result.error {
+ ErrorPresenter.showErrorAlert(error: error, from: self)
+ } else {
+ let tunnel: TunnelContainer = result.value!
+ self?.dismiss(self)
+ self?.delegate?.tunnelSaved(tunnel: tunnel)
}
- } catch let error as WireGuardAppError {
- ErrorPresenter.showErrorAlert(error: error, from: self)
- } catch {
- fatalError()
}
}
}