Skip to content

Commit 0c0ebf6

Browse files
committed
Turn Ingestion.fetchMetadata into throws(Github.Error)
1 parent 04ef182 commit 0c0ebf6

File tree

4 files changed

+23
-15
lines changed

4 files changed

+23
-15
lines changed

Sources/App/Commands/Ingest.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,25 @@ extension Ingestion {
250250
}
251251

252252

253-
static func fetchMetadata(client: Client, package: Package, owner: String, repository: String) async throws -> (Github.Metadata, Github.License?, Github.Readme?) {
253+
static func fetchMetadata(client: Client, package: Package, owner: String, repository: String) async throws(Github.Error) -> (Github.Metadata, Github.License?, Github.Readme?) {
254254
async let metadata = try await Current.fetchMetadata(client, owner, repository)
255255
async let license = await Current.fetchLicense(client, owner, repository)
256256
async let readme = await Current.fetchReadme(client, owner, repository)
257257

258-
return try await (metadata, license, readme)
258+
do {
259+
return try await (metadata, license, readme)
260+
} catch let error as Github.Error {
261+
throw error
262+
} catch {
263+
// This whole do { ... } catch { ... } should be unnecessary - it's a workaround for
264+
// https://github.com/swiftlang/swift/issues/76169
265+
assert(false, "Unexpected error type: \(type(of: error))")
266+
// We need to throw _something_ here (we should never hit this codepath though)
267+
throw Github.Error.requestFailed(.internalServerError)
268+
// We could theoretically avoid this whole second catch and just do
269+
// error as! GithubError
270+
// but let's play it safe and not risk a server crash, unlikely as it may be.
271+
}
259272
}
260273
}
261274

Sources/App/Core/AppEnvironment.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import FoundationNetworking
2525
struct AppEnvironment: Sendable {
2626
var fetchHTTPStatusCode: @Sendable (_ url: String) async throws -> HTTPStatus
2727
var fetchLicense: @Sendable (_ client: Client, _ owner: String, _ repository: String) async -> Github.License?
28-
var fetchMetadata: @Sendable (_ client: Client, _ owner: String, _ repository: String) async throws -> Github.Metadata
28+
var fetchMetadata: @Sendable (_ client: Client, _ owner: String, _ repository: String) async throws(Github.Error) -> Github.Metadata
2929
var fetchReadme: @Sendable (_ client: Client, _ owner: String, _ repository: String) async -> Github.Readme?
3030
var fetchS3Readme: @Sendable (_ client: Client, _ owner: String, _ repository: String) async throws -> String
3131
var fileManager: FileManager
@@ -86,7 +86,7 @@ extension AppEnvironment {
8686
static let live = AppEnvironment(
8787
fetchHTTPStatusCode: { url in try await Networking.fetchHTTPStatusCode(url) },
8888
fetchLicense: { client, owner, repo in await Github.fetchLicense(client:client, owner: owner, repository: repo) },
89-
fetchMetadata: { client, owner, repo in try await Github.fetchMetadata(client:client, owner: owner, repository: repo) },
89+
fetchMetadata: { client, owner, repo throws(Github.Error) in try await Github.fetchMetadata(client:client, owner: owner, repository: repo) },
9090
fetchReadme: { client, owner, repo in await Github.fetchReadme(client:client, owner: owner, repository: repo) },
9191
fetchS3Readme: { client, owner, repo in try await S3Readme.fetchReadme(client:client, owner: owner, repository: repo) },
9292
fileManager: .live,

Tests/AppTests/ErrorReportingTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class ErrorReportingTests: AppTestCase {
3434
func test_Ingestor_error_reporting() async throws {
3535
// setup
3636
try await Package(id: .id0, url: "1", processingStage: .reconciliation).save(on: app.db)
37-
Current.fetchMetadata = { _, _, _ in throw Github.Error.invalidURL("1") }
37+
Current.fetchMetadata = { _, _, _ throws(Github.Error) in throw Github.Error.invalidURL("1") }
3838

3939
try await withDependencies {
4040
$0.date.now = .now

Tests/AppTests/IngestorTests.swift

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,12 @@ class IngestorTests: AppTestCase {
6565
func test_ingest_continue_on_error() async throws {
6666
// Test completion of ingestion despite early error
6767
// setup
68-
enum TestError: Error, Equatable {
69-
case badRequest
70-
}
71-
7268
let packages = try await savePackages(on: app.db, ["https://github.com/foo/1",
7369
"https://github.com/foo/2"], processingStage: .reconciliation)
7470
.map(Joined<Package, Repository>.init(model:))
75-
Current.fetchMetadata = { _, owner, repository in
71+
Current.fetchMetadata = { _, owner, repository throws(Github.Error) in
7672
if owner == "foo" && repository == "1" {
77-
throw TestError.badRequest
73+
throw Github.Error.requestFailed(.badRequest)
7874
}
7975
return .mock(owner: owner, repository: repository)
8076
}
@@ -328,11 +324,10 @@ class IngestorTests: AppTestCase {
328324
let urls = ["https://github.com/foo/1",
329325
"https://github.com/foo/2",
330326
"https://github.com/foo/3"]
331-
let packages = try await savePackages(on: app.db, urls.asURLs,
332-
processingStage: .reconciliation)
333-
Current.fetchMetadata = { _, owner, repository in
327+
try await savePackages(on: app.db, urls.asURLs, processingStage: .reconciliation)
328+
Current.fetchMetadata = { _, owner, repository throws(Github.Error) in
334329
if owner == "foo" && repository == "2" {
335-
throw AppError.genericError(packages[1].id, "error 2")
330+
throw Github.Error.requestFailed(.badRequest)
336331
}
337332
return .mock(owner: owner, repository: repository)
338333
}

0 commit comments

Comments
 (0)