Skip to content
Merged
Changes from 2 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)
case unexpectedResponseBody(expectedContent: String, body: Any)

// 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) 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)
}
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.