From bcef906c471df944d1682da735ca61dbcc2f334d Mon Sep 17 00:00:00 2001 From: Si Beaumont Date: Thu, 21 Sep 2023 23:07:46 +0100 Subject: [PATCH 1/3] Add SPIs for throwing RuntimeError on unexpected output --- .../OpenAPIRuntime/Errors/RuntimeError.swift | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Sources/OpenAPIRuntime/Errors/RuntimeError.swift b/Sources/OpenAPIRuntime/Errors/RuntimeError.swift index 4304b4cd..4adad4dd 100644 --- a/Sources/OpenAPIRuntime/Errors/RuntimeError.swift +++ b/Sources/OpenAPIRuntime/Errors/RuntimeError.swift @@ -55,6 +55,10 @@ internal enum RuntimeError: Error, CustomStringConvertible, LocalizedError, Pret case transportFailed(any Error) case handlerFailed(any Error) + // Unexpected response (thrown by shorthand APIs) + case unexpectedResponseStatus(expectedStatus: String, response: Any) + case unexpectedResponseBody(expectedContent: String, body: Any) + // MARK: CustomStringConvertible var description: String { @@ -96,6 +100,20 @@ internal enum RuntimeError: Error, CustomStringConvertible, LocalizedError, Pret return "Transport failed with error: \(underlyingError.localizedDescription)" case .handlerFailed(let underlyingError): return "User handler failed with error: \(underlyingError.localizedDescription)" + case .unexpectedResponseStatus(let expectedStatus, let response): + return "Unexpected response, expected status code: \(expectedStatus), response: \(response)" + case .unexpectedResponseBody(let expectedContentType, let response): + return "Unexpected response body, expected content type: \(expectedContentType), response: \(response)" } } } + +@_spi(Generated) +public func throwUnexpectedResponseStatus(expectedStatus: String, response: Any) throws -> Never { + throw RuntimeError.unexpectedResponseStatus(expectedStatus: expectedStatus, response: response) +} + +@_spi(Generated) +public func throwUnexpectedResponseBody(expectedContent: String, body: Any) throws -> Never { + throw RuntimeError.unexpectedResponseBody(expectedContent: expectedContent, body: body) +} From 5f6099eae86b64ddc3f4b53d89ea64da233138d4 Mon Sep 17 00:00:00 2001 From: Si Beaumont Date: Mon, 2 Oct 2023 14:47:35 +0100 Subject: [PATCH 2/3] Update Sources/OpenAPIRuntime/Errors/RuntimeError.swift Co-authored-by: Honza Dvorsky --- Sources/OpenAPIRuntime/Errors/RuntimeError.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/OpenAPIRuntime/Errors/RuntimeError.swift b/Sources/OpenAPIRuntime/Errors/RuntimeError.swift index 4adad4dd..fb82a4a7 100644 --- a/Sources/OpenAPIRuntime/Errors/RuntimeError.swift +++ b/Sources/OpenAPIRuntime/Errors/RuntimeError.swift @@ -102,8 +102,8 @@ internal enum RuntimeError: Error, CustomStringConvertible, LocalizedError, Pret return "User handler failed with error: \(underlyingError.localizedDescription)" case .unexpectedResponseStatus(let expectedStatus, let response): return "Unexpected response, expected status code: \(expectedStatus), response: \(response)" - case .unexpectedResponseBody(let expectedContentType, let response): - return "Unexpected response body, expected content type: \(expectedContentType), response: \(response)" + case .unexpectedResponseBody(let expectedContentType, let body): + return "Unexpected response body, expected content type: \(expectedContentType), body: \(body)" } } } From c39d002361533288a1e9ac9d63e3d07f60350bd9 Mon Sep 17 00:00:00 2001 From: Si Beaumont Date: Mon, 2 Oct 2023 19:30:34 +0100 Subject: [PATCH 3/3] fixup: Replace Any function param with any Sendable Signed-off-by: Si Beaumont --- Sources/OpenAPIRuntime/Errors/RuntimeError.swift | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/OpenAPIRuntime/Errors/RuntimeError.swift b/Sources/OpenAPIRuntime/Errors/RuntimeError.swift index fb82a4a7..9c1fb0be 100644 --- a/Sources/OpenAPIRuntime/Errors/RuntimeError.swift +++ b/Sources/OpenAPIRuntime/Errors/RuntimeError.swift @@ -56,8 +56,8 @@ internal enum RuntimeError: Error, CustomStringConvertible, LocalizedError, Pret case handlerFailed(any Error) // Unexpected response (thrown by shorthand APIs) - case unexpectedResponseStatus(expectedStatus: String, response: Any) - case unexpectedResponseBody(expectedContent: String, body: Any) + case unexpectedResponseStatus(expectedStatus: String, response: any Sendable) + case unexpectedResponseBody(expectedContent: String, body: any Sendable) // MARK: CustomStringConvertible @@ -109,11 +109,11 @@ internal enum RuntimeError: Error, CustomStringConvertible, LocalizedError, Pret } @_spi(Generated) -public func throwUnexpectedResponseStatus(expectedStatus: String, response: Any) throws -> Never { +public func throwUnexpectedResponseStatus(expectedStatus: String, response: any Sendable) throws -> Never { throw RuntimeError.unexpectedResponseStatus(expectedStatus: expectedStatus, response: response) } @_spi(Generated) -public func throwUnexpectedResponseBody(expectedContent: String, body: Any) throws -> Never { +public func throwUnexpectedResponseBody(expectedContent: String, body: any Sendable) throws -> Never { throw RuntimeError.unexpectedResponseBody(expectedContent: expectedContent, body: body) }