Skip to content

Commit e4fc9e6

Browse files
committed
Rename MASError.runtimeError as error.
Signed-off-by: Ross Goldberg <[email protected]>
1 parent 146d26a commit e4fc9e6

File tree

12 files changed

+35
-40
lines changed

12 files changed

+35
-40
lines changed

Sources/mas/AppStore/AppStoreAction.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ enum AppStoreAction {
5757
return
5858
}
5959
guard response?.downloads?.isEmpty == false else {
60-
continuation.resume(throwing: MASError.runtimeError("No downloads initiated for ADAM ID \(adamID)"))
60+
continuation.resume(throwing: MASError.error("No downloads initiated for ADAM ID \(adamID)"))
6161
return
6262
}
6363

Sources/mas/AppStore/DownloadQueueObserver.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,11 @@ final class DownloadQueueObserver: CKDownloadQueueObserver {
163163
try manuallyInstall(appNameAndVersion: metadata.appNameAndVersion)
164164
} else {
165165
guard !status.isFailed else {
166-
throw MASError.runtimeError("Failed to download \(metadata.appNameAndVersion)")
166+
throw MASError.error("Failed to download \(metadata.appNameAndVersion)")
167167
}
168168
guard !status.isCancelled else {
169169
guard shouldCancel(download, false) else {
170-
throw MASError.runtimeError("Download cancelled for \(metadata.appNameAndVersion)")
170+
throw MASError.error("Download cancelled for \(metadata.appNameAndVersion)")
171171
}
172172

173173
completionHandler?()
@@ -221,18 +221,18 @@ final class DownloadQueueObserver: CKDownloadQueueObserver {
221221

222222
private func manuallyInstall(appNameAndVersion: String) throws {
223223
guard pkgHardLinkURL != nil else {
224-
throw MASError.runtimeError("Failed to find pkg to install for \(appNameAndVersion)")
224+
throw MASError.error("Failed to find pkg to install for \(appNameAndVersion)")
225225
}
226226
guard receiptHardLinkURL != nil else {
227-
throw MASError.runtimeError("Failed to find receipt to import for \(appNameAndVersion)")
227+
throw MASError.error("Failed to find receipt to import for \(appNameAndVersion)")
228228
}
229229

230230
try spotlightImport(appNameAndVersion: appNameAndVersion, from: try install(appNameAndVersion: appNameAndVersion))
231231
}
232232

233233
private func install(appNameAndVersion: String) throws -> URL {
234234
guard let pkgHardLinkPath = pkgHardLinkURL?.path else {
235-
throw MASError.runtimeError("Failed to find pkg to install for \(appNameAndVersion)")
235+
throw MASError.error("Failed to find pkg to install for \(appNameAndVersion)")
236236
}
237237

238238
let process = Process()
@@ -245,15 +245,15 @@ final class DownloadQueueObserver: CKDownloadQueueObserver {
245245
do {
246246
try run(asEffectiveUID: 0, andEffectiveGID: 0) { try process.run() }
247247
} catch {
248-
throw MASError.runtimeError("Failed to install \(appNameAndVersion) from \(pkgHardLinkPath)", error: error)
248+
throw MASError.error("Failed to install \(appNameAndVersion) from \(pkgHardLinkPath)", error: error)
249249
}
250250
process.waitUntilExit()
251251
let standardOutputText =
252252
String(data: standardOutputPipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) ?? ""
253253
let standardErrorText =
254254
String(data: standardErrorPipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) ?? ""
255255
guard process.terminationStatus == 0 else {
256-
throw MASError.runtimeError(
256+
throw MASError.error(
257257
"""
258258
Failed to install \(appNameAndVersion) from \(pkgHardLinkPath)
259259
Exit status: \(process.terminationStatus)\(
@@ -269,15 +269,15 @@ final class DownloadQueueObserver: CKDownloadQueueObserver {
269269
appFolderURLNSRange.location != NSNotFound,
270270
let appFolderURLRange = Range(appFolderURLNSRange, in: standardErrorText)
271271
else { // swiftformat:enable indent
272-
throw MASError.runtimeError(
272+
throw MASError.error(
273273
"Failed to find app folder URL in installer output for \(appNameAndVersion)",
274274
error: standardErrorText
275275
)
276276
}
277277

278278
let appFolderURLString = String(standardErrorText[appFolderURLRange])
279279
guard let appFolderURL = URL(string: appFolderURLString) else {
280-
throw MASError.runtimeError(
280+
throw MASError.error(
281281
"Failed to parse app folder URL for \(appNameAndVersion) from \(appFolderURLString)",
282282
error: standardErrorText
283283
)
@@ -288,10 +288,10 @@ final class DownloadQueueObserver: CKDownloadQueueObserver {
288288

289289
private func spotlightImport(appNameAndVersion: String, from appFolderURL: URL) throws {
290290
guard let receiptHardLinkURL else {
291-
throw MASError.runtimeError("Failed to find receipt to import for \(appNameAndVersion)")
291+
throw MASError.error("Failed to find receipt to import for \(appNameAndVersion)")
292292
}
293293
guard let pkgHardLinkPath = pkgHardLinkURL?.path else {
294-
throw MASError.runtimeError("Failed to find pkg to install for \(appNameAndVersion)")
294+
throw MASError.error("Failed to find pkg to install for \(appNameAndVersion)")
295295
}
296296

297297
let receiptURL = appFolderURL.appendingPathComponent("Contents/_MASReceipt/receipt", isDirectory: false)
@@ -303,7 +303,7 @@ final class DownloadQueueObserver: CKDownloadQueueObserver {
303303
try fileManager.setAttributes([.ownerAccountID: 0, .groupOwnerAccountID: 0], ofItemAtPath: receiptURL.path)
304304
}
305305
} catch {
306-
throw MASError.runtimeError(
306+
throw MASError.error(
307307
"""
308308
Failed to copy receipt for \(appNameAndVersion) from \(receiptHardLinkURL.path.quoted) to\
309309
\(receiptURL.path.quoted)
@@ -322,11 +322,11 @@ final class DownloadQueueObserver: CKDownloadQueueObserver {
322322
do {
323323
try process.run()
324324
} catch {
325-
throw MASError.runtimeError("Failed to install \(appNameAndVersion) from \(pkgHardLinkPath)", error: error)
325+
throw MASError.error("Failed to install \(appNameAndVersion) from \(pkgHardLinkPath)", error: error)
326326
}
327327
process.waitUntilExit()
328328
guard process.terminationStatus == 0 else {
329-
throw MASError.runtimeError(
329+
throw MASError.error(
330330
"""
331331
Failed to install \(appNameAndVersion) from \(pkgHardLinkPath)
332332
Exit status: \(process.terminationStatus)\(
@@ -402,12 +402,12 @@ private extension URL {
402402
return false
403403
}
404404
guard let fileResourceID1 = try resourceValues(forKeys: [.fileResourceIdentifierKey]).fileResourceIdentifier else {
405-
throw MASError.runtimeError("Failed to get file resource identifier for \(path)")
405+
throw MASError.error("Failed to get file resource identifier for \(path)")
406406
}
407407
guard
408408
let fileResourceID2 = try url.resourceValues(forKeys: [.fileResourceIdentifierKey]).fileResourceIdentifier
409409
else {
410-
throw MASError.runtimeError("Failed to get file resource identifier for \(url.path)")
410+
throw MASError.error("Failed to get file resource identifier for \(url.path)")
411411
}
412412

413413
return fileResourceID1.isEqual(fileResourceID2)

Sources/mas/Commands/Open.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ extension MAS {
6363

6464
private func openMacAppStore() async throws {
6565
guard let macAppStoreSchemeURL = URL(string: "macappstore:") else {
66-
throw MASError.runtimeError("Failed to create URL from macappstore scheme")
66+
throw MASError.error("Failed to create URL from macappstore scheme")
6767
}
6868

6969
let workspace = NSWorkspace.shared
7070
guard let appURL = workspace.urlForApplication(toOpen: macAppStoreSchemeURL) else {
71-
throw MASError.runtimeError("Failed to find app to open macappstore URLs")
71+
throw MASError.error("Failed to find app to open macappstore URLs")
7272
}
7373

7474
try await workspace.openApplication(at: appURL, configuration: NSWorkspace.OpenConfiguration())

Sources/mas/Commands/Uninstall.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ extension MAS {
6565
let uid = try processInfo.sudoUID
6666
let gid = try processInfo.sudoGID
6767
guard let finder = SBApplication(bundleIdentifier: "com.apple.finder") as (any FinderApplication)? else {
68-
throw MASError.runtimeError("Failed to obtain Finder access: bundle com.apple.finder does not exist")
68+
throw MASError.error("Failed to obtain Finder access: bundle com.apple.finder does not exist")
6969
}
7070
guard let items = finder.items else {
71-
throw MASError.runtimeError("Failed to obtain Finder access: FinderApplication.items() does not exist")
71+
throw MASError.error("Failed to obtain Finder access: FinderApplication.items() does not exist")
7272
}
7373

7474
let fileManager = FileManager.default

Sources/mas/Controllers/ITunesSearchAppCatalog.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ struct ITunesSearchAppCatalog: AppCatalog {
110110
do {
111111
return try JSONDecoder().decode(CatalogAppResults.self, from: data).results
112112
} catch {
113-
throw MASError.runtimeError(
113+
throw MASError.error(
114114
"Failed to parse response from \(url) as JSON",
115115
error: String(data: data, encoding: .utf8) ?? ""
116116
)

Sources/mas/Controllers/SpotlightInstalledApps.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ var installedApps: [InstalledApp] {
3939
) { notification in
4040
guard let query = notification.object as? NSMetadataQuery else {
4141
continuation.resume(
42-
throwing: MASError.runtimeError(
42+
throwing: MASError.error(
4343
"Notification Center returned a \(type(of: notification.object)) instead of a NSMetadataQuery"
4444
)
4545
)

Sources/mas/Errors/MASError.swift

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,21 @@
88
internal import Foundation
99

1010
enum MASError: Error {
11+
case error(String, error: (any Error)? = nil, separator: String = ":\n", separatorAndErrorReplacement: String = "")
1112
case noCatalogAppsFound(for: String)
12-
case runtimeError(
13-
String,
14-
error: (any Error)? = nil,
15-
separator: String = ":\n",
16-
separatorAndErrorReplacement: String = ""
17-
)
1813
case unknownAppID(AppID)
1914
case unparsableURL(String)
2015
case unsupportedCommand(String)
2116

22-
static func runtimeError(
17+
static func error(
2318
_ message: String,
2419
error: String?,
2520
separator: String = ":\n",
2621
separatorAndErrorReplacement: String = ""
2722
) -> Self {
28-
runtimeError(
23+
Self.error(
2924
message,
30-
error: error.map { runtimeError($0) },
25+
error: error.map { Self.error($0) },
3126
separator: separator,
3227
separatorAndErrorReplacement: separatorAndErrorReplacement
3328
)
@@ -37,10 +32,10 @@ enum MASError: Error {
3732
extension MASError: CustomStringConvertible {
3833
var description: String {
3934
switch self {
35+
case let .error(message, error, separator, separatorAndErrorReplacement):
36+
"\(message)\(error.map { "\(separator)\($0)" } ?? separatorAndErrorReplacement)"
4037
case let .noCatalogAppsFound(searchTerm):
4138
"No apps found in the App Store for search term: \(searchTerm)"
42-
case let .runtimeError(message, error, separator, separatorAndErrorReplacement):
43-
"\(message)\(error.map { "\(separator)\($0)" } ?? separatorAndErrorReplacement)"
4439
case let .unknownAppID(appID):
4540
"No apps found in the App Store for \(appID)"
4641
case let .unparsableURL(string):

Sources/mas/Utilities/Group.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func groupName(for gid: gid_t) -> String? {
2020

2121
func set(effectiveGID gid: gid_t) throws {
2222
guard setegid(gid) == 0 else {
23-
throw MASError.runtimeError("Failed to switch effective group from \(getegid().nameAndID) to \(gid.nameAndID)")
23+
throw MASError.error("Failed to switch effective group from \(getegid().nameAndID) to \(gid.nameAndID)")
2424
}
2525
}
2626

Sources/mas/Utilities/ProcessInfo.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extension ProcessInfo {
1212
var sudoUID: uid_t {
1313
get throws {
1414
guard let sudoUID = environment["SUDO_UID"].flatMap({ uid_t($0) }) else {
15-
throw MASError.runtimeError("Failed to get sudo uid")
15+
throw MASError.error("Failed to get sudo uid")
1616
}
1717

1818
return sudoUID
@@ -22,7 +22,7 @@ extension ProcessInfo {
2222
var sudoGID: gid_t {
2323
get throws {
2424
guard let sudoGID = environment["SUDO_GID"].flatMap({ gid_t($0) }) else {
25-
throw MASError.runtimeError("Failed to get sudo gid")
25+
throw MASError.error("Failed to get sudo gid")
2626
}
2727

2828
return sudoGID

Sources/mas/Utilities/Sudo.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ private func sudo(_ args: some Sequence<String>) throws {
3232
var pid = 0 as pid_t
3333
let spawnStatus = posix_spawn(&pid, "/usr/bin/sudo", nil, nil, cArgs + [nil], environ)
3434
guard spawnStatus == 0 else {
35-
throw MASError.runtimeError(
35+
throw MASError.error(
3636
"Failed to spawn installer process",
3737
error: String(cString: strerror(spawnStatus), encoding: .utf8),
3838
separator: ": "

0 commit comments

Comments
 (0)