Skip to content

Commit

Permalink
Merge branch 'jens/no-more-space-error'
Browse files Browse the repository at this point in the history
  • Loading branch information
jensutbult committed Oct 24, 2024
2 parents ee23a6a + 925db18 commit fb640f5
Showing 1 changed file with 41 additions and 11 deletions.
52 changes: 41 additions & 11 deletions Authenticator/UI/ErrorAlertView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import SwiftUI

extension View {
func errorAlert(error: Binding<Error?>, buttonTitle: String = String(localized: "OK", comment:"OK button in error alert."), handler: (() -> Void)? = nil) -> some View {
let localizedAlertError = LocalizedAlertError(error: error.wrappedValue)
let localizedAlertError = error.wrappedValue.map { LocalizedErrorWrapper(error: $0) }

return alert(isPresented: .constant(localizedAlertError != nil), error: localizedAlertError) { _ in
Button(buttonTitle) {
error.wrappedValue = nil
Expand All @@ -30,24 +31,53 @@ extension View {
}
}

struct LocalizedAlertError: LocalizedError {
let underlyingError: LocalizedError
struct LocalizedErrorWrapper: LocalizedError {

enum ErrorType {
case nsError(NSError), localizedError(LocalizedError), error(Error)
}

let underlyingError: ErrorType

var errorDescription: String? {
underlyingError.errorDescription
switch underlyingError {
case .nsError(let nsError):
return nsError.customDescription()
case .localizedError(let localizedError):
return localizedError.errorDescription
case .error(let error):
return "Unknown error: \(error)"
}
}
var recoverySuggestion: String? {
underlyingError.recoverySuggestion
switch underlyingError {
case .nsError(let nsError):
return nsError.localizedRecoverySuggestion
case .localizedError(let localizedError):
return localizedError.recoverySuggestion
case .error(_):
return nil
}
}

init?(error: Error?) {
guard let localizedError = error as? LocalizedError else { return nil }
underlyingError = localizedError
init(error: Error) {
if type(of: error) is NSError.Type {
underlyingError = .nsError(error as NSError)
} else if let localizedError = error as? LocalizedError {
underlyingError = .localizedError(localizedError)
} else {
underlyingError = .error(error)
}
}
}

extension NSError: LocalizedError {
public var errorDescription: String? {
return self.localizedDescription
extension NSError {
func customDescription() -> String {
if self.domain == "com.yubico" {
if self.code == 0x6a84 {
return String(localized: "The YubiKey has no more storage for OATH accounts.")
}
}
return localizedDescription
}
}

0 comments on commit fb640f5

Please sign in to comment.