Skip to content

Commit 944971b

Browse files
committed
Rename run(_:throwing:) to run(_:rethrowing), bundle with run(_:defer:)
1 parent 13029df commit 944971b

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

Sources/App/Commands/Ingest.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ extension Ingestion {
185185

186186
try await run { () async throws(Ingestion.Error.UnderlyingError) in
187187
try await updateRepository(on: database, for: repo, metadata: metadata, licenseInfo: license, readmeInfo: readme, s3Readme: s3Readme, fork: fork)
188-
} throwing: {
188+
} rethrowing: {
189189
Ingestion.Error(packageId: package.model.id!, underlyingError: $0)
190190
}
191191
return package
@@ -209,7 +209,7 @@ extension Ingestion {
209209
static func findOrCreateRepository(on database: Database, for package: Joined<Package, Repository>) async throws(Ingestion.Error) -> Repository {
210210
try await run {
211211
try await Repository.findOrCreate(on: database, for: package.model)
212-
} throwing: {
212+
} rethrowing: {
213213
Ingestion.Error(
214214
packageId: package.model.id!,
215215
underlyingError: .findOrCreateRepositoryFailed(url: package.model.url, details: $0)
@@ -241,7 +241,7 @@ func fetchMetadata(client: Client, package: Joined<Package, Repository>) async t
241241
// The only way to get `owner` and `repository` here is by parsing them from the URL.
242242
let (owner, repository) = try await run {
243243
try Github.parseOwnerName(url: package.model.url)
244-
} throwing: { _ in
244+
} rethrowing: { _ in
245245
Ingestion.Error.invalidURL(packageId: package.model.id!, url: package.model.url)
246246
}
247247

Sources/App/Core/AsyncDefer.swift renamed to Sources/App/Core/ErrorHandlingHelpers.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
@discardableResult
1717
func run<T>(_ operation: () async throws -> T,
18-
defer deferredOperation: () async throws -> Void) async throws -> T {
18+
defer deferredOperation: () async throws -> Void) async throws -> T {
1919
do {
2020
let result = try await operation()
2121
try await deferredOperation()
@@ -25,3 +25,15 @@ func run<T>(_ operation: () async throws -> T,
2525
throw error
2626
}
2727
}
28+
29+
30+
@discardableResult
31+
func run<T, E1: Error, E2: Error>(_ operation: () async throws(E1) -> T,
32+
rethrowing transform: (E1) -> E2) async throws(E2) -> T {
33+
do {
34+
let result = try await operation()
35+
return result
36+
} catch {
37+
throw transform(error)
38+
}
39+
}

Sources/App/Core/Extensions/Result+ext.swift

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,3 @@ extension Result where Failure == Error {
2525

2626
var isError: Bool { return !isSucess }
2727
}
28-
29-
30-
// Not really a part of the Result type but closely enough related to put here
31-
// Perhaps put this in AsyncDefer.swift and rename the file?
32-
@discardableResult
33-
func run<T, E1: Error, E2: Error>(_ operation: () async throws(E1) -> T,
34-
throwing transform: (E1) -> E2) async throws(E2) -> T {
35-
do {
36-
let result = try await operation()
37-
return result
38-
} catch {
39-
throw transform(error)
40-
}
41-
}

0 commit comments

Comments
 (0)