Skip to content

Commit eebc540

Browse files
committed
Refactor multipart parsing errors
1 parent 94da7f6 commit eebc540

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

Diff for: Tests/ApolloTests/Interceptors/MultipartResponseParsingInterceptorTests.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ final class MultipartResponseParsingInterceptorTests: XCTestCase {
4545

4646
expect(result).to(beFailure { error in
4747
expect(error).to(
48-
matchError(MultipartResponseParsingInterceptor.ParsingError.cannotParseResponse)
48+
matchError(MultipartResponseParsingInterceptor.ParsingError.missingMultipartBoundary)
4949
)
5050
})
5151
}
5252

5353
wait(for: [expectation], timeout: defaultTimeout)
5454
}
5555

56-
func test__error__givenResponse_withMissingMultipartProtocolHeader_shouldReturnError() throws {
56+
func test__error__givenResponse_withMissingMultipartProtocolSpecifier_shouldReturnError() throws {
5757
let subject = InterceptorTester(interceptor: MultipartResponseParsingInterceptor())
5858

5959
let expectation = expectation(description: "Received callback")
@@ -68,15 +68,15 @@ final class MultipartResponseParsingInterceptorTests: XCTestCase {
6868

6969
expect(result).to(beFailure { error in
7070
expect(error).to(
71-
matchError(MultipartResponseParsingInterceptor.ParsingError.cannotParseResponse)
71+
matchError(MultipartResponseParsingInterceptor.ParsingError.invalidMultipartProtocol)
7272
)
7373
})
7474
}
7575

7676
wait(for: [expectation], timeout: defaultTimeout)
7777
}
7878

79-
func test__error__givenResponse_withUnknownMultipartParser_shouldReturnError() throws {
79+
func test__error__givenResponse_withUnknownMultipartProtocolSpecifier_shouldReturnError() throws {
8080
let subject = InterceptorTester(interceptor: MultipartResponseParsingInterceptor())
8181

8282
let expectation = expectation(description: "Received callback")
@@ -91,7 +91,7 @@ final class MultipartResponseParsingInterceptorTests: XCTestCase {
9191

9292
expect(result).to(beFailure { error in
9393
expect(error).to(
94-
matchError(MultipartResponseParsingInterceptor.ParsingError.cannotParseResponse)
94+
matchError(MultipartResponseParsingInterceptor.ParsingError.invalidMultipartProtocol)
9595
)
9696
})
9797
}

Diff for: apollo-ios/Sources/Apollo/MultipartResponseParsingInterceptor.swift

+18-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ public struct MultipartResponseParsingInterceptor: ApolloInterceptor {
88

99
public enum ParsingError: Error, LocalizedError, Equatable {
1010
case noResponseToParse
11+
@available(*, deprecated, message: "Use the more specific `missingMultipartBoundary` and `invalidMultipartProtocol` errors instead.")
1112
case cannotParseResponse
1213
case cannotParseResponseData
14+
case missingMultipartBoundary
15+
case invalidMultipartProtocol
1316

1417
public var errorDescription: String? {
1518
switch self {
@@ -19,6 +22,10 @@ public struct MultipartResponseParsingInterceptor: ApolloInterceptor {
1922
return "The response data could not be parsed."
2023
case .cannotParseResponseData:
2124
return "The response data could not be parsed."
25+
case .missingMultipartBoundary:
26+
return "Missing multipart boundary in the response 'content-type' header."
27+
case .invalidMultipartProtocol:
28+
return "Missing, or unknown, multipart specification protocol in the response 'content-type' header."
2229
}
2330
}
2431
}
@@ -61,13 +68,22 @@ public struct MultipartResponseParsingInterceptor: ApolloInterceptor {
6168

6269
let multipartComponents = response.httpResponse.multipartHeaderComponents
6370

71+
guard let boundary = multipartComponents.boundary else {
72+
chain.handleErrorAsync(
73+
ParsingError.missingMultipartBoundary,
74+
request: request,
75+
response: response,
76+
completion: completion
77+
)
78+
return
79+
}
80+
6481
guard
65-
let boundary = multipartComponents.boundary,
6682
let `protocol` = multipartComponents.protocol,
6783
let parser = Self.responseParsers[`protocol`]
6884
else {
6985
chain.handleErrorAsync(
70-
ParsingError.cannotParseResponse,
86+
ParsingError.invalidMultipartProtocol,
7187
request: request,
7288
response: response,
7389
completion: completion

0 commit comments

Comments
 (0)