From 0a2d1e9d377167559dcda44d0e59b70d10e7ee0b Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Thu, 7 Jul 2022 16:43:40 +0200 Subject: [PATCH] Mitigate fatalError() in properties, fail gracefully Do not trigger fatalError() on properties too much because SwiftUI may read them at any unpredictable time. --- Passepartout/App/Views/ProfileView+Provider.swift | 12 +++++++----- .../Extensions/Host+Extensions.swift | 15 ++------------- .../DefaultVPNManager+Configuration.swift | 4 ++-- 3 files changed, 11 insertions(+), 20 deletions(-) diff --git a/Passepartout/App/Views/ProfileView+Provider.swift b/Passepartout/App/Views/ProfileView+Provider.swift index ee74e296..b23acdc4 100644 --- a/Passepartout/App/Views/ProfileView+Provider.swift +++ b/Passepartout/App/Views/ProfileView+Provider.swift @@ -81,7 +81,7 @@ extension ProfileView { Text(L10n.Profile.Items.Provider.Refresh.caption) }.withTrailingProgress(when: isRefreshingInfrastructure) } header: { - Text(currentProvider.fullName) + currentProviderFullName.map(Text.init) } footer: { lastInfrastructureUpdate.map { Text(L10n.Profile.Sections.ProviderInfrastructure.footer($0)) @@ -89,14 +89,16 @@ extension ProfileView { } } - private var currentProvider: ProviderMetadata { + private var currentProviderFullName: String? { guard let name = currentProfile.value.header.providerName else { - fatalError("Provider name accessed but profile is not a provider (isPlaceholder? \(currentProfile.value.isPlaceholder))") + assertionFailure("Provider name accessed but profile is not a provider (isPlaceholder? \(currentProfile.value.isPlaceholder))") + return nil } guard let metadata = providerManager.provider(withName: name) else { - fatalError("Provider metadata not found") + assertionFailure("Provider metadata not found") + return nil } - return metadata + return metadata.name } // private var currentProviderLocation: String? { diff --git a/PassepartoutLibrary/Sources/PassepartoutCore/Extensions/Host+Extensions.swift b/PassepartoutLibrary/Sources/PassepartoutCore/Extensions/Host+Extensions.swift index 34c46e49..bf3fe7a0 100644 --- a/PassepartoutLibrary/Sources/PassepartoutCore/Extensions/Host+Extensions.swift +++ b/PassepartoutLibrary/Sources/PassepartoutCore/Extensions/Host+Extensions.swift @@ -49,30 +49,18 @@ extension Profile { public var hostOpenVPNSettings: OpenVPNSettings? { get { - guard host != nil else { - fatalError("Not a host") - } return host?.ovpnSettings } set { - guard host != nil else { - fatalError("Not a host") - } host?.ovpnSettings = newValue } } public var hostWireGuardSettings: WireGuardSettings? { get { - guard host != nil else { - fatalError("Not a host") - } return host?.wgSettings } set { - guard host != nil else { - fatalError("Not a host") - } host?.wgSettings = newValue } } @@ -95,7 +83,8 @@ extension Profile.Host: ProfileSubtype { } else if let _ = wgSettings { return [.wireGuard] } else { - fatalError("No VPN settings found") + assertionFailure("No VPN settings found") + return [] } } } diff --git a/PassepartoutLibrary/Sources/PassepartoutVPN/Managers/DefaultVPNManager+Configuration.swift b/PassepartoutLibrary/Sources/PassepartoutVPN/Managers/DefaultVPNManager+Configuration.swift index f4eda44f..1cd9f889 100644 --- a/PassepartoutLibrary/Sources/PassepartoutVPN/Managers/DefaultVPNManager+Configuration.swift +++ b/PassepartoutLibrary/Sources/PassepartoutVPN/Managers/DefaultVPNManager+Configuration.swift @@ -75,7 +75,7 @@ extension DefaultVPNManager { settings = try profile.providerOpenVPNSettings(withManager: providerManager) } else { guard let hostSettings = profile.hostOpenVPNSettings else { - fatalError("Host has no OpenVPN settings") + fatalError("Profile currentVPNProtocol is OpenVPN, but host has no OpenVPN settings") } settings = hostSettings } @@ -87,7 +87,7 @@ extension DefaultVPNManager { settings = try profile.providerWireGuardSettings(withManager: providerManager) } else { guard let hostSettings = profile.hostWireGuardSettings else { - fatalError("Host has no WireGuard settings") + fatalError("Profile currentVPNProtocol is WireGuard, but host has no WireGuard settings") } settings = hostSettings }