Skip to content

Commit 0030548

Browse files
Add SPIs for throwing RuntimeError from shorthand generated APIs (#56)
### Motivation The review period for SOAR-0007 (Shorthand APIs for inputs and outputs) has now concluded. This pull request adds the required SPIs to the runtime library to throw a runtime error when the response and/or body does not match that of the shorthand API being used. For further context, please review the proposal itself.[^1] [^1]: apple/swift-openapi-generator#291 ### Modifications - Extend `internal enum RuntimeError: Error` with two new cases: - `.unexpectedResponseStatus(expectedStatus:response:)` - `.unexpectedResponseBody(expectedContent:body:)` - Add SPI for generated code, to throw these errors: - `@_spi(Generated) public throwUnexpectedResponseStatus(expectedStatus:response:)` - `@_spi(Generated) public throwUnexpectedResponseBody(expectedStatus:body:)` ### Result Runtime library has two SPIs that can be used by the generator to implement the shorthand throwing getter APIs described in SOAR-0007. ### Test Plan Companion PR in swift-openapi-generator. --------- Signed-off-by: Si Beaumont <beaumont@apple.com> Co-authored-by: Honza Dvorsky <honza@apple.com>
1 parent 1eaf236 commit 0030548

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Sources/OpenAPIRuntime/Errors/RuntimeError.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ internal enum RuntimeError: Error, CustomStringConvertible, LocalizedError, Pret
5555
case transportFailed(any Error)
5656
case handlerFailed(any Error)
5757

58+
// Unexpected response (thrown by shorthand APIs)
59+
case unexpectedResponseStatus(expectedStatus: String, response: any Sendable)
60+
case unexpectedResponseBody(expectedContent: String, body: any Sendable)
61+
5862
// MARK: CustomStringConvertible
5963

6064
var description: String {
@@ -96,6 +100,20 @@ internal enum RuntimeError: Error, CustomStringConvertible, LocalizedError, Pret
96100
return "Transport failed with error: \(underlyingError.localizedDescription)"
97101
case .handlerFailed(let underlyingError):
98102
return "User handler failed with error: \(underlyingError.localizedDescription)"
103+
case .unexpectedResponseStatus(let expectedStatus, let response):
104+
return "Unexpected response, expected status code: \(expectedStatus), response: \(response)"
105+
case .unexpectedResponseBody(let expectedContentType, let body):
106+
return "Unexpected response body, expected content type: \(expectedContentType), body: \(body)"
99107
}
100108
}
101109
}
110+
111+
@_spi(Generated)
112+
public func throwUnexpectedResponseStatus(expectedStatus: String, response: any Sendable) throws -> Never {
113+
throw RuntimeError.unexpectedResponseStatus(expectedStatus: expectedStatus, response: response)
114+
}
115+
116+
@_spi(Generated)
117+
public func throwUnexpectedResponseBody(expectedContent: String, body: any Sendable) throws -> Never {
118+
throw RuntimeError.unexpectedResponseBody(expectedContent: expectedContent, body: body)
119+
}

0 commit comments

Comments
 (0)