Skip to content

Commit

Permalink
Merge pull request #809 from stripe/bdorfman-fix-error-parsing
Browse files Browse the repository at this point in the history
Fix error parsing
  • Loading branch information
bdorfman-stripe authored Oct 2, 2017
2 parents ddb7382 + aeb0527 commit 2154ceb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
11 changes: 5 additions & 6 deletions Stripe/STPAPIRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,12 @@ + (void)parseResponse:(NSURLResponse *)response
}
}
}
if (!deserializerClass) {
// No deserializer for response body
return safeCompletion(nil, [NSError stp_genericFailedToParseResponseError]);
}

// Generate response object
id<STPAPIResponseDecodable> responseObject = [deserializerClass decodedObjectFromAPIResponse:jsonDictionary];
id<STPAPIResponseDecodable> responseObject = nil;
if (deserializerClass) {
// Generate response object
responseObject = [deserializerClass decodedObjectFromAPIResponse:jsonDictionary];
}

if (!responseObject) {
// Failed to parse response
Expand Down
31 changes: 30 additions & 1 deletion Tests/Tests/STPAPIRequestTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ - (void)testParseResponseWithConnectionError {
[self waitForExpectationsWithTimeout:2.0 handler:nil];
}

- (void)testParseResponseWithReturnedError {
- (void)testParseResponseWithReturnedErrorOneDeserializer {
XCTestExpectation *expectation = [self expectationWithDescription:@"parseResponse"];

NSHTTPURLResponse *httpURLResponse = [[NSHTTPURLResponse alloc] init];
Expand Down Expand Up @@ -382,6 +382,35 @@ - (void)testParseResponseWithReturnedError {
[self waitForExpectationsWithTimeout:2.0 handler:nil];
}

- (void)testParseResponseWithReturnedErrorMultipleDeserializers {
XCTestExpectation *expectation = [self expectationWithDescription:@"parseResponse"];

NSHTTPURLResponse *httpURLResponse = [[NSHTTPURLResponse alloc] init];
NSDictionary *json = @{
@"error": @{
@"type": @"invalid_request_error",
@"message": @"Your card number is incorrect.",
@"code": @"incorrect_number",
}
};
NSData *body = [NSJSONSerialization dataWithJSONObject:json options:(NSJSONWritingOptions)kNilOptions error:nil];
NSError *errorParameter = nil;
NSArray *deserializers = @[[STPCard new], [STPSource new]];

[STPAPIRequest parseResponse:httpURLResponse
body:body
error:errorParameter
deserializers:deserializers
completion:^(id<STPAPIResponseDecodable> object, NSHTTPURLResponse *response, NSError *error) {
XCTAssertNil(object);
XCTAssertEqualObjects(response, httpURLResponse);
XCTAssertEqualObjects(error, [NSError stp_errorFromStripeResponse:json]);
[expectation fulfill];
}];

[self waitForExpectationsWithTimeout:2.0 handler:nil];
}

- (void)testParseResponseWithMissingError {
XCTestExpectation *expectation = [self expectationWithDescription:@"parseResponse"];

Expand Down

0 comments on commit 2154ceb

Please sign in to comment.