Skip to content
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

Issue 2426 improve response code error api #3123

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
11 changes: 11 additions & 0 deletions Sources/Apollo/ResponseCodeInterceptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ public struct ResponseCodeInterceptor: ApolloInterceptor {
return errorStrings.joined(separator: " ")
}
}

public var graphQLError: GraphQLError? {
switch self {
case .invalidResponseCode(_, let rawData):
if let jsonRawData = rawData,
let jsonData = try? JSONSerialization.jsonObject(with: jsonRawData, options: .allowFragments) as? JSONObject {
return GraphQLError(jsonData)
}
return nil
}
}
}

/// Designated initializer
Expand Down
87 changes: 83 additions & 4 deletions Tests/ApolloTests/Interceptors/ResponseCodeInterceptorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,96 @@ class ResponseCodeInterceptorTests: XCTestCase {
guard
let data = rawData,
let dataString = String(bytes: data, encoding: .utf8) else {
XCTFail("Incorrect data returned with error")
return
XCTFail("Incorrect data returned with error")
return
}


guard
let castError = error as? ResponseCodeInterceptor.ResponseCodeError,
let dataEntry = castError.graphQLError?["data"] as? JSONObject,
let heroEntry = dataEntry["hero"] as? JSONObject,
let typeName = heroEntry["__typename"] as? String,
let heroName = heroEntry["name"] as? String
else {
XCTFail("Invalid GraphQL Error")
return
}
XCTAssertEqual("GraphQL Error", castError.graphQLError?.description)
XCTAssertEqual("Human", typeName)
XCTAssertEqual("Luke Skywalker", heroName)

XCTAssertEqual(dataString, "{\"data\":{\"hero\":{\"__typename\":\"Human\",\"name\":\"Luke Skywalker\"}}}")
default:
XCTFail("Unexpected error type: \(error.localizedDescription)")
}
}
}


self.wait(for: [expectation], timeout: 1)
}

func testResponseCodeInterceptorDoesNotHaveGraphQLError() {
class TestProvider: InterceptorProvider {
let mockClient: MockURLSessionClient = {
let client = MockURLSessionClient()
client.response = HTTPURLResponse(url: TestURL.mockServer.url,
statusCode: 401,
httpVersion: nil,
headerFields: nil)
client.data = "Not a GraphQL Error".data(using: .utf8)
return client
}()

func interceptors<Operation: GraphQLOperation>(
for operation: Operation
) -> [any ApolloInterceptor] {
[
NetworkFetchInterceptor(client: self.mockClient),
ResponseCodeInterceptor(),
JSONResponseParsingInterceptor(),
]
}
}

let network = RequestChainNetworkTransport(interceptorProvider: TestProvider(),
endpointURL: TestURL.mockServer.url)

let expectation = self.expectation(description: "Request sent")

_ = network.send(operation: MockQuery.mock()) { result in
defer {
expectation.fulfill()
}

switch result {
case .success:
XCTFail("This should not have succeeded")
case .failure(let error):
switch error {
case ResponseCodeInterceptor.ResponseCodeError.invalidResponseCode(response: let response, let rawData):
XCTAssertEqual(response?.statusCode, 401)

guard
let data = rawData,
let dataString = String(bytes: data, encoding: .utf8) else {
XCTFail("Incorrect data returned with error")
return
}

guard let castError = error as? ResponseCodeInterceptor.ResponseCodeError else {
XCTFail("It should be a ResponseCodeError")
return
}

XCTAssertNil(castError.graphQLError)

XCTAssertEqual(dataString, "Not a GraphQL Error")
default:
XCTFail("Unexpected error type: \(error.localizedDescription)")
}
}
}

self.wait(for: [expectation], timeout: 1)
}
}