Mitigate fatalError() in properties, fail gracefully

Do not trigger fatalError() on properties too much because
SwiftUI may read them at any unpredictable time.
This commit is contained in:
Davide De Rosa 2022-07-07 16:43:40 +02:00 committed by Davide De Rosa
parent a442603696
commit 0a2d1e9d37
3 changed files with 11 additions and 20 deletions

View File

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

View File

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

View File

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