Skip to content

Commit

Permalink
Add SPIs for throwing RuntimeError from shorthand generated APIs (#56)
Browse files Browse the repository at this point in the history
### 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 <[email protected]>
Co-authored-by: Honza Dvorsky <[email protected]>
  • Loading branch information
simonjbeaumont and czechboy0 authored Oct 3, 2023
1 parent 1eaf236 commit 0030548
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Sources/OpenAPIRuntime/Errors/RuntimeError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 Sendable)
case unexpectedResponseBody(expectedContent: String, body: any Sendable)

// MARK: CustomStringConvertible

var description: String {
Expand Down Expand Up @@ -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 body):
return "Unexpected response body, expected content type: \(expectedContentType), body: \(body)"
}
}
}

@_spi(Generated)
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 Sendable) throws -> Never {
throw RuntimeError.unexpectedResponseBody(expectedContent: expectedContent, body: body)
}

0 comments on commit 0030548

Please sign in to comment.