aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard/Tunnel/TunnelErrors.swift
blob: 45c20f6eea1972c9e1565d5bcb3d74af5cbc8ba8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// SPDX-License-Identifier: MIT
// Copyright © 2018 WireGuard LLC. All Rights Reserved.

import NetworkExtension

enum TunnelsManagerError: WireGuardAppError {
    case tunnelNameEmpty
    case tunnelAlreadyExistsWithThatName
    case systemErrorOnListingTunnels(systemError: Error)
    case systemErrorOnAddTunnel(systemError: Error)
    case systemErrorOnModifyTunnel(systemError: Error)
    case systemErrorOnRemoveTunnel(systemError: Error)

    var alertText: AlertText {
        switch self {
        case .tunnelNameEmpty:
            return ("No name provided", "Cannot create tunnel with an empty name")
        case .tunnelAlreadyExistsWithThatName:
            return ("Name already exists", "A tunnel with that name already exists")
        case .systemErrorOnListingTunnels(let systemError):
            return ("Unable to list tunnels", systemError.UIString)
        case .systemErrorOnAddTunnel(let systemError):
            return ("Unable to create tunnel", systemError.UIString)
        case .systemErrorOnModifyTunnel(let systemError):
            return ("Unable to modify tunnel", systemError.UIString)
        case .systemErrorOnRemoveTunnel(let systemError):
            return ("Unable to remove tunnel", systemError.UIString)
        }
    }
}

enum TunnelsManagerActivationAttemptError: WireGuardAppError {
    case tunnelIsNotInactive
    case failedWhileStarting(systemError: Error) // startTunnel() throwed
    case failedWhileSaving(systemError: Error) // save config after re-enabling throwed
    case failedWhileLoading(systemError: Error) // reloading config throwed
    case failedBecauseOfTooManyErrors(lastSystemError: Error) // recursion limit reached

    var alertText: AlertText {
        switch self {
        case .tunnelIsNotInactive:
            return ("Activation failure", "The tunnel is already active or in the process of being activated")
        case .failedWhileStarting(let systemError),
             .failedWhileSaving(let systemError),
             .failedWhileLoading(let systemError),
             .failedBecauseOfTooManyErrors(let systemError):
            return ("Activation failure", "The tunnel could not be activated. " + systemError.UIString)
        }
    }
}

enum TunnelsManagerActivationError: WireGuardAppError {
    case activationFailed
    case activationFailedWithExtensionError(title: String, message: String)
    
    var alertText: AlertText {
        switch self {
        case .activationFailed:
            return ("Activation failure", "The tunnel could not be activated. Please ensure that you are connected to the Internet.")
        case .activationFailedWithExtensionError(let title, let message):
            return (title, message)
        }
    }
}

extension Error {
    var UIString: String {
        if let systemError = self as? NEVPNError {
            switch systemError {
            case NEVPNError.configurationInvalid:
                return "The configuration is invalid."
            case NEVPNError.configurationDisabled:
                return "The configuration is disabled."
            case NEVPNError.connectionFailed:
                return "The connection failed."
            case NEVPNError.configurationStale:
                return "The configuration is stale."
            case NEVPNError.configurationReadWriteFailed:
                return "Reading or writing the configuration failed."
            case NEVPNError.configurationUnknown:
                return "Unknown system error."
            default:
                return ""
            }
        } else {
            return localizedDescription
        }
    }
}