Files
SideStore/SideStoreApp/Sources/SideStoreUIKit/Managing Apps/AppManagerErrors.swift

70 lines
2.3 KiB
Swift
Raw Normal View History

//
// AppManagerErrors.swift
// AltStore
//
// Created by Riley Testut on 8/27/20.
// Copyright © 2020 Riley Testut. All rights reserved.
//
import CoreData
2023-03-01 00:48:36 -05:00
import Foundation
2023-03-01 00:48:36 -05:00
import SideStoreCore
2023-03-01 19:09:33 -05:00
public extension AppManager {
2023-03-01 00:48:36 -05:00
struct FetchSourcesError: LocalizedError, CustomNSError {
2023-03-01 19:09:33 -05:00
public private(set) var primaryError: Error?
2023-03-01 00:48:36 -05:00
2023-03-01 19:09:33 -05:00
public private(set) var sources: Set<Source>?
public private(set) var errors = [Source: Error]()
2023-03-01 00:48:36 -05:00
2023-03-01 19:09:33 -05:00
public private(set) var managedObjectContext: NSManagedObjectContext?
2023-03-01 00:48:36 -05:00
2023-03-01 19:09:33 -05:00
public var errorDescription: String? {
2023-03-01 00:48:36 -05:00
if let error = primaryError {
return error.localizedDescription
2023-03-01 00:48:36 -05:00
} else {
var localizedDescription: String?
2023-03-01 00:48:36 -05:00
managedObjectContext?.performAndWait {
if self.sources?.count == 1 {
localizedDescription = NSLocalizedString("Could not refresh store.", comment: "")
2023-03-01 00:48:36 -05:00
} else if self.errors.count == 1 {
guard let source = self.errors.keys.first else { return }
localizedDescription = String(format: NSLocalizedString("Could not refresh source “%@”.", comment: ""), source.name)
2023-03-01 00:48:36 -05:00
} else {
localizedDescription = String(format: NSLocalizedString("Could not refresh %@ sources.", comment: ""), NSNumber(value: self.errors.count))
}
}
2023-03-01 00:48:36 -05:00
return localizedDescription
}
}
2023-03-01 00:48:36 -05:00
2023-03-01 19:09:33 -05:00
public var recoverySuggestion: String? {
2023-03-01 00:48:36 -05:00
if let error = primaryError as NSError? {
return error.localizedRecoverySuggestion
2023-03-01 00:48:36 -05:00
} else if errors.count == 1 {
return nil
2023-03-01 00:48:36 -05:00
} else {
return NSLocalizedString("Tap to view source errors.", comment: "")
}
}
2023-03-01 00:48:36 -05:00
2023-03-01 19:09:33 -05:00
public var errorUserInfo: [String: Any] {
2023-03-01 00:48:36 -05:00
guard let error = errors.values.first, errors.count == 1 else { return [:] }
return [NSUnderlyingErrorKey: error]
}
2023-03-01 00:48:36 -05:00
2023-03-01 19:09:33 -05:00
public init(_ error: Error) {
2023-03-01 00:48:36 -05:00
primaryError = error
}
2023-03-01 00:48:36 -05:00
2023-03-01 19:09:33 -05:00
public init(sources: Set<Source>, errors: [Source: Error], context: NSManagedObjectContext) {
self.sources = sources
self.errors = errors
2023-03-01 00:48:36 -05:00
managedObjectContext = context
}
}
}