Skip to content

Commit bcef906

Browse files
Add SPIs for throwing RuntimeError on unexpected output
1 parent 1eaf236 commit bcef906

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)
60+
case unexpectedResponseBody(expectedContent: String, body: Any)
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 response):
106+
return "Unexpected response body, expected content type: \(expectedContentType), response: \(response)"
99107
}
100108
}
101109
}
110+
111+
@_spi(Generated)
112+
public func throwUnexpectedResponseStatus(expectedStatus: String, response: Any) throws -> Never {
113+
throw RuntimeError.unexpectedResponseStatus(expectedStatus: expectedStatus, response: response)
114+
}
115+
116+
@_spi(Generated)
117+
public func throwUnexpectedResponseBody(expectedContent: String, body: Any) throws -> Never {
118+
throw RuntimeError.unexpectedResponseBody(expectedContent: expectedContent, body: body)
119+
}

0 commit comments

Comments
 (0)