From 06040516c8d5b68fa04dce26884ba0e4272c2f26 Mon Sep 17 00:00:00 2001 From: Diego Rey Mendez Date: Thu, 2 May 2024 17:37:40 +0200 Subject: [PATCH] Adds underlying error info to WireGuardAdapterError and NetworkProtectionClientError --- .../Networking/NetworkProtectionClient.swift | 21 +++++++++++++ .../WireGuardKit/WireGuardAdapter.swift | 31 ++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/Sources/NetworkProtection/Networking/NetworkProtectionClient.swift b/Sources/NetworkProtection/Networking/NetworkProtectionClient.swift index 4531f29a7..b61b58efc 100644 --- a/Sources/NetworkProtection/Networking/NetworkProtectionClient.swift +++ b/Sources/NetworkProtection/Networking/NetworkProtectionClient.swift @@ -79,6 +79,27 @@ public enum NetworkProtectionClientError: CustomNSError, NetworkProtectionErrorC case .accessDenied: return 13 } } + + public var errorUserInfo: [String: Any] { + switch self { + case .failedToFetchLocationList(let error), + .failedToParseLocationListResponse(let error), + .failedToFetchServerList(let error), + .failedToParseServerListResponse(let error), + .failedToFetchRegisteredServers(let error), + .failedToParseRegisteredServersResponse(let error), + .failedToRedeemInviteCode(let error), + .failedToParseRedeemResponse(let error): + return [NSUnderlyingErrorKey: error as NSError] + case .failedToEncodeRegisterKeyRequest, + .failedToEncodeRedeemRequest, + .invalidInviteCode, + .failedToRetrieveAuthToken, + .invalidAuthToken, + .accessDenied: + return [:] + } + } } struct RegisterKeyRequestBody: Encodable { diff --git a/Sources/NetworkProtection/WireGuardKit/WireGuardAdapter.swift b/Sources/NetworkProtection/WireGuardKit/WireGuardAdapter.swift index 0b416abd3..c430673b7 100644 --- a/Sources/NetworkProtection/WireGuardKit/WireGuardAdapter.swift +++ b/Sources/NetworkProtection/WireGuardKit/WireGuardAdapter.swift @@ -13,7 +13,7 @@ public enum WireGuardAdapterErrorInvalidStateReason: String { case updatedTunnelWhileStopped } -public enum WireGuardAdapterError: Error { +public enum WireGuardAdapterError: CustomNSError { /// Failure to locate tunnel file descriptor. case cannotLocateTunnelFileDescriptor @@ -28,6 +28,35 @@ public enum WireGuardAdapterError: Error { /// Failure to start WireGuard backend. case startWireGuardBackend(Int32) + + public var errorCode: Int { + switch self { + case .cannotLocateTunnelFileDescriptor: return 100 + case .invalidState: return 101 + case .dnsResolution: return 102 + case .setNetworkSettings: return 103 + case .startWireGuardBackend: return 104 + } + } + + public var errorUserInfo: [String: Any] { + switch self { + case .cannotLocateTunnelFileDescriptor, + .invalidState: + return [:] + case .dnsResolution(let errors): + guard let firstError = errors.first else { + return [:] + } + + return [NSUnderlyingErrorKey: firstError as NSError] + case .setNetworkSettings(let error): + return [NSUnderlyingErrorKey: error as NSError] + case .startWireGuardBackend(let code): + let error = NSError(domain: "startWireGuardBackend", code: Int(code)) + return [NSUnderlyingErrorKey: error as NSError] + } + } } /// Enum representing internal state of the `WireGuardAdapter`