forked from altstoreio/AltStore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoggedError.swift
More file actions
147 lines (125 loc) · 4.81 KB
/
LoggedError.swift
File metadata and controls
147 lines (125 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//
// LoggedError.swift
// AltStoreCore
//
// Created by Riley Testut on 9/6/22.
// Copyright © 2022 Riley Testut. All rights reserved.
//
import CoreData
extension LoggedError
{
public enum Operation: String
{
case install
case update
case refresh
case activate
case deactivate
case backup
case restore
case enableJIT
}
}
@objc(LoggedError)
public class LoggedError: NSManagedObject, Fetchable
{
/* Properties */
@NSManaged public private(set) var date: Date
@nonobjc public var operation: Operation? {
guard let rawOperation = self._operation else { return nil }
let operation = Operation(rawValue: rawOperation)
return operation
}
@NSManaged @objc(operation) private var _operation: String?
@NSManaged public private(set) var domain: String
@NSManaged public private(set) var code: Int32
@NSManaged public private(set) var userInfo: [String: Any]
@NSManaged public private(set) var appName: String
@NSManaged public private(set) var appBundleID: String
/* Relationships */
@NSManaged public private(set) var storeApp: StoreApp?
@NSManaged public private(set) var installedApp: InstalledApp?
private static let dateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .long
dateFormatter.timeStyle = .none
return dateFormatter
}()
private override init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?)
{
super.init(entity: entity, insertInto: context)
}
public init(error: Error, app: AppProtocol, date: Date = Date(), operation: Operation? = nil, context: NSManagedObjectContext)
{
super.init(entity: LoggedError.entity(), insertInto: context)
self.date = date
self._operation = operation?.rawValue
let nsError: NSError
if let error = error as? ALTServerError, error.code == .underlyingError, let underlyingError = error.underlyingError
{
nsError = underlyingError as NSError
}
else
{
nsError = error as NSError
}
self.domain = nsError.domain
self.code = Int32(nsError.code)
self.userInfo = nsError.userInfo
self.appName = app.name
self.appBundleID = app.bundleIdentifier
switch app
{
case let storeApp as StoreApp: self.storeApp = storeApp
case let installedApp as InstalledApp: self.installedApp = installedApp
case let appVersion as AppVersion:
if let installedApp = appVersion.app?.installedApp
{
self.installedApp = installedApp
}
else
{
self.storeApp = appVersion.app
}
default: break
}
}
}
public extension LoggedError
{
var app: AppProtocol {
// `as AppProtocol` needed to fix "cannot convert AnyApp to StoreApp" compiler error with Xcode 14.
let app = self.installedApp ?? self.storeApp ?? AnyApp(name: self.appName, bundleIdentifier: self.appBundleID, url: nil) as AppProtocol
return app
}
var error: NSError {
let nsError = NSError(domain: self.domain, code: Int(self.code), userInfo: self.userInfo)
return nsError
}
@objc
var localizedDateString: String {
let localizedDateString = LoggedError.dateFormatter.string(from: self.date)
return localizedDateString
}
var localizedFailure: String? {
guard let operation = self.operation else { return nil }
switch operation
{
case .install: return String(format: NSLocalizedString("Install %@ Failed", comment: ""), self.appName)
case .update: return String(format: NSLocalizedString("Update %@ Failed", comment: ""), self.appName)
case .refresh: return String(format: NSLocalizedString("Refresh %@ Failed", comment: ""), self.appName)
case .activate: return String(format: NSLocalizedString("Activate %@ Failed", comment: ""), self.appName)
case .deactivate: return String(format: NSLocalizedString("Deactivate %@ Failed", comment: ""), self.appName)
case .backup: return String(format: NSLocalizedString("Backup %@ Failed", comment: ""), self.appName)
case .restore: return String(format: NSLocalizedString("Restore %@ Failed", comment: ""), self.appName)
case .enableJIT: return String(format: NSLocalizedString("Enable JIT for %@ Failed", comment: ""), self.appName)
}
}
}
public extension LoggedError
{
@nonobjc class func fetchRequest() -> NSFetchRequest<LoggedError>
{
return NSFetchRequest<LoggedError>(entityName: "LoggedError")
}
}