Skip to content

Add SPIs for throwing RuntimeError from shorthand generated APIs #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these could go into ErrorExtensions.swift, and maybe return the constructed error rather than throw it (similar to DecodingError.failedToDecodeAnySchema(...) etc).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to explore a better home for these, but is ErrorExtensions.swift a good fit?

In ErrorExtensions.swift we extend the public type DecodingError. Here we are creating an internal RuntimeError. Had this been a public type, I'd have made these SPIs extensions of RuntimeError.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, maybe the whole file should be renamed to ErrorUtilities.swift or something, or a new ErrorFactories.swift file could hold these. Will leave up to you.