Skip to content

Commit 7fab570

Browse files
committed
fail open api document opening as soon as an API request to retrieve it fails instead of attempting to parse a failed response. log openapi source prepping/retrieval errors. use the basic auth defined in environment variables if available.
1 parent d755f11 commit 7fab570

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

Sources/APITesting/APITestCommand.swift

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,16 @@ public final class APITestCommand: Command {
127127
)
128128
.flatMap { trackProgress(testProgressTracker?.markBuilding()) }
129129
.flatMap { openAPIDoc(on: eventLoop, from: source) }
130+
.flatMapError { error in
131+
let errorString: String
132+
if let error = error as? Abort {
133+
errorString = "HTTP Error: \(error.status.code) - \(error.status.reasonPhrase)"
134+
} else {
135+
errorString = OpenAPI.Error(from: error).localizedDescription
136+
}
137+
testLogger.error(path: nil, context: "Prepping/Retrieving OpenAPI Source", message: errorString)
138+
return eventLoop.makeFailedFuture(error)
139+
}
130140
.flatMap { openAPIDoc in
131141
produceAPITestPackage(
132142
on: eventLoop,
@@ -149,10 +159,6 @@ public final class APITestCommand: Command {
149159
requestLogger?.info("Cleaning up tests in \(outPath)")
150160
}
151161
.recover { error in
152-
// For requests with the ability to distinguish between request
153-
// logging and test logging, only log this "summary" message
154-
// to the request logger. For any other request, log it to the
155-
// test logger.
156162
if let requestLogger = requestLogger {
157163
requestLogger.error("Testing Failed",
158164
metadata: ["error": .stringConvertible(String(describing: error))])
@@ -251,7 +257,12 @@ public func openAPIDoc(
251257
return loop.makeFailedFuture(Abort(.badRequest))
252258
}
253259

254-
return client.execute(request: request).flatMapThrowing { response in
260+
return client.execute(request: request).flatMap { (response) -> EventLoopFuture<HTTPClient.Response> in
261+
guard response.status == .ok else {
262+
return loop.makeFailedFuture(Abort(response.status))
263+
}
264+
return loop.makeSucceededFuture(response)
265+
}.flatMapThrowing { response in
255266
return try ClientResponse(status: response.status, headers: response.headers, body: response.body)
256267
.content.decode(OpenAPI.Document.self)
257268
}.always { _ in try! client.syncShutdown() }

Sources/App/Models/DB/OpenAPISource+DB.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,13 @@ extension OpenAPISource {
5555
case .filepath:
5656
self = .file(path: dbModel.uri)
5757
case .url:
58-
self = .unauthenticated(url: URI(string: dbModel.uri))
58+
let url = URI(string: dbModel.uri)
59+
if url.string == Environment.inUrl, let credentials = try? Environment.credentials() {
60+
self = .basicAuth(url: url, username: credentials.username, password: credentials.password)
61+
}
62+
else {
63+
self = .unauthenticated(url: url)
64+
}
5965
}
6066
}
6167

0 commit comments

Comments
 (0)