Skip to content

Improve error message when encountering unexpected content-type headers #104

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
Show file tree
Hide file tree
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
7 changes: 6 additions & 1 deletion Sources/OpenAPIRuntime/Conversion/Converter+Common.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ extension Converter {
// The force unwrap is safe, we only get here if the array is not empty.
let bestOption = evaluatedOptions.max { a, b in a.match.score < b.match.score }!
let bestContentType = bestOption.contentType
if case .incompatible = bestOption.match { throw RuntimeError.unexpectedContentTypeHeader(bestContentType) }
if case .incompatible = bestOption.match {
throw RuntimeError.unexpectedContentTypeHeader(
expected: bestContentType,
received: String(describing: received)
)
}
return bestContentType
}

Expand Down
5 changes: 3 additions & 2 deletions Sources/OpenAPIRuntime/Errors/RuntimeError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal enum RuntimeError: Error, CustomStringConvertible, LocalizedError, Pret

// Headers
case missingRequiredHeaderField(String)
case unexpectedContentTypeHeader(String)
case unexpectedContentTypeHeader(expected: String, received: String)
case unexpectedAcceptHeader(String)
case malformedAcceptHeader(String)
case missingOrMalformedContentDispositionName
Expand Down Expand Up @@ -95,7 +95,8 @@ internal enum RuntimeError: Error, CustomStringConvertible, LocalizedError, Pret
return
"Unsupported parameter style, parameter name: '\(name)', kind: \(location), style: \(style), explode: \(explode)"
case .missingRequiredHeaderField(let name): return "The required header field named '\(name)' is missing."
case .unexpectedContentTypeHeader(let contentType): return "Unexpected Content-Type header: \(contentType)"
case .unexpectedContentTypeHeader(expected: let expected, received: let received):
return "Unexpected content type, expected: \(expected), received: \(received)"
case .unexpectedAcceptHeader(let accept): return "Unexpected Accept header: \(accept)"
case .malformedAcceptHeader(let accept): return "Malformed Accept header: \(accept)"
case .missingOrMalformedContentDispositionName:
Expand Down
13 changes: 11 additions & 2 deletions Tests/OpenAPIRuntimeTests/Conversion/Test_Converter+Common.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//
//===----------------------------------------------------------------------===//
import XCTest
@_spi(Generated) import OpenAPIRuntime
@testable @_spi(Generated) import OpenAPIRuntime
import HTTPTypes

extension HTTPField.Name { static var foo: Self { Self("foo")! } }
Expand Down Expand Up @@ -84,7 +84,16 @@ final class Test_CommonConverterExtensions: Test_Runtime {
try testCase(received: "image/png", options: ["image/*", "*/*"], expected: "image/*")
XCTAssertThrowsError(
try testCase(received: "text/csv", options: ["text/html", "application/json"], expected: "-")
)
) { error in
XCTAssert(error is RuntimeError)
guard let error = error as? RuntimeError,
case .unexpectedContentTypeHeader(expected: let expected, received: let received) = error,
expected == "text/html", received == "text/csv"
else {
XCTFail("Unexpected error: \(error)")
return
}
}
}

func testVerifyContentTypeIfPresent() throws {
Expand Down