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

Add injecting additionalErrorHandler for upload operations to RequestChainNetworkTransport #2948

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
19 changes: 13 additions & 6 deletions Sources/Apollo/RequestChainNetworkTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ open class RequestChainNetworkTransport: NetworkTransport {
callbackQueue: DispatchQueue = .main,
completionHandler: @escaping (Result<GraphQLResult<Operation.Data>, Error>) -> Void) -> Cancellable {

let interceptors = self.interceptorProvider.interceptors(for: operation)
let chain = InterceptorRequestChain(interceptors: interceptors, callbackQueue: callbackQueue)
chain.additionalErrorHandler = self.interceptorProvider.additionalErrorInterceptor(for: operation)
let chain = makeChain(operation: operation, callbackQueue: callbackQueue)
let request = self.constructRequest(for: operation,
cachePolicy: cachePolicy,
contextIdentifier: contextIdentifier)
Expand All @@ -124,6 +122,17 @@ open class RequestChainNetworkTransport: NetworkTransport {
chain.kickoff(request: request, completion: completionHandler)
return chain
}

private func makeChain<Operation: GraphQLOperation>(
operation: Operation,
callbackQueue: DispatchQueue = .main
) -> RequestChain {
let interceptors = self.interceptorProvider.interceptors(for: operation)
let chain = InterceptorRequestChain(interceptors: interceptors, callbackQueue: callbackQueue)
chain.additionalErrorHandler = self.interceptorProvider.additionalErrorInterceptor(for: operation)
return chain
}

}

extension RequestChainNetworkTransport: UploadingNetworkTransport {
Expand Down Expand Up @@ -159,9 +168,7 @@ extension RequestChainNetworkTransport: UploadingNetworkTransport {
completionHandler: @escaping (Result<GraphQLResult<Operation.Data>, Error>) -> Void) -> Cancellable {

let request = self.constructUploadRequest(for: operation, with: files)
let interceptors = self.interceptorProvider.interceptors(for: operation)
let chain = InterceptorRequestChain(interceptors: interceptors, callbackQueue: callbackQueue)

let chain = makeChain(operation: operation, callbackQueue: callbackQueue)
chain.kickoff(request: request, completion: completionHandler)
return chain
}
Expand Down
79 changes: 78 additions & 1 deletion Tests/ApolloTests/RequestChainTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class RequestChainTests: XCTestCase {
self.wait(for: [expectation], timeout: 2)
}

func testErrorInterceptorGetsCalledAfterAnErrorIsReceived() {
func test__send__ErrorInterceptorGetsCalledAfterAnErrorIsReceived() {
class ErrorInterceptor: ApolloErrorInterceptor {
var error: Error? = nil

Expand Down Expand Up @@ -136,6 +136,83 @@ class RequestChainTests: XCTestCase {
XCTFail("Error interceptor did not receive an error!")
}
}

func test__upload__ErrorInterceptorGetsCalledAfterAnErrorIsReceived() throws {
class ErrorInterceptor: ApolloErrorInterceptor {
var error: Error? = nil

func handleErrorAsync<Operation: GraphQLOperation>(
error: Error,
chain: RequestChain,
request: HTTPRequest<Operation>,
response: HTTPResponse<Operation>?,
completion: @escaping (Result<GraphQLResult<Operation.Data>, Error>) -> Void) {

self.error = error
completion(.failure(error))
}
}

class TestProvider: InterceptorProvider {
let errorInterceptor = ErrorInterceptor()
func interceptors<Operation: GraphQLOperation>(for operation: Operation) -> [ApolloInterceptor] {
return [
// An interceptor which will error without a response
ResponseCodeInterceptor()
]
}

func additionalErrorInterceptor<Operation: GraphQLOperation>(for operation: Operation) -> ApolloErrorInterceptor? {
return self.errorInterceptor
}
}

let provider = TestProvider()
let transport = RequestChainNetworkTransport(interceptorProvider: provider,
endpointURL: TestURL.mockServer.url,
autoPersistQueries: true)

let fileURL = TestFileHelper.fileURLForFile(named: "a", extension: "txt")
let file = try GraphQLFile(
fieldName: "file",
originalName: "a.txt",
fileURL: fileURL
)

let expectation = self.expectation(description: "Hero name query complete")
_ = transport.upload(operation: MockQuery.mock(), files: [file]) { 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:
// This is what we want.
break
default:
XCTFail("Unexpected error: \(error)")
}
}
}

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

switch provider.errorInterceptor.error {
case .some(let error):
switch error {
case ResponseCodeInterceptor.ResponseCodeError.invalidResponseCode:
// Again, this is what we expect.
break
default:
XCTFail("Unexpected error on the interceptor: \(error)")
}
case .none:
XCTFail("Error interceptor did not receive an error!")
}
}

func testErrorInterceptorGetsCalledInDefaultInterceptorProviderSubclass() {
class ErrorInterceptor: ApolloErrorInterceptor {
Expand Down