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) Text(L10n.Profile.Items.Provider.Refresh.caption)
}.withTrailingProgress(when: isRefreshingInfrastructure) }.withTrailingProgress(when: isRefreshingInfrastructure)
} header: { } header: {
Text(currentProvider.fullName) currentProviderFullName.map(Text.init)
} footer: { } footer: {
lastInfrastructureUpdate.map { lastInfrastructureUpdate.map {
Text(L10n.Profile.Sections.ProviderInfrastructure.footer($0)) 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 { 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 { 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? { // private var currentProviderLocation: String? {

View File

@ -49,30 +49,18 @@ extension Profile {
public var hostOpenVPNSettings: OpenVPNSettings? { public var hostOpenVPNSettings: OpenVPNSettings? {
get { get {
guard host != nil else {
fatalError("Not a host")
}
return host?.ovpnSettings return host?.ovpnSettings
} }
set { set {
guard host != nil else {
fatalError("Not a host")
}
host?.ovpnSettings = newValue host?.ovpnSettings = newValue
} }
} }
public var hostWireGuardSettings: WireGuardSettings? { public var hostWireGuardSettings: WireGuardSettings? {
get { get {
guard host != nil else {
fatalError("Not a host")
}
return host?.wgSettings return host?.wgSettings
} }
set { set {
guard host != nil else {
fatalError("Not a host")
}
host?.wgSettings = newValue host?.wgSettings = newValue
} }
} }
@ -95,7 +83,8 @@ extension Profile.Host: ProfileSubtype {
} else if let _ = wgSettings { } else if let _ = wgSettings {
return [.wireGuard] return [.wireGuard]
} else { } 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) settings = try profile.providerOpenVPNSettings(withManager: providerManager)
} else { } else {
guard let hostSettings = profile.hostOpenVPNSettings 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 settings = hostSettings
} }
@ -87,7 +87,7 @@ extension DefaultVPNManager {
settings = try profile.providerWireGuardSettings(withManager: providerManager) settings = try profile.providerWireGuardSettings(withManager: providerManager)
} else { } else {
guard let hostSettings = profile.hostWireGuardSettings 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 settings = hostSettings
} }