mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-22 20:18:53 +08:00
Fix failed assertion when constructing an unprocessable response error for 5xx errors. closes #1169
This commit is contained in:
@@ -48,11 +48,22 @@ NSError *RKErrorFromMappingResult(RKMappingResult *mappingResult)
|
||||
return error;
|
||||
}
|
||||
|
||||
static NSError *RKUnprocessableClientErrorFromResponse(NSHTTPURLResponse *response)
|
||||
static NSIndexSet *RKErrorStatusCodes()
|
||||
{
|
||||
NSCAssert(NSLocationInRange(response.statusCode, RKStatusCodeRangeForClass(RKStatusCodeClassClientError)), @"Expected response status code to be in the 400-499 range, instead got %ld", (long) response.statusCode);
|
||||
static NSIndexSet *errorStatusCodes = nil;
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
errorStatusCodes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(400, 200)];
|
||||
});
|
||||
|
||||
return errorStatusCodes;
|
||||
}
|
||||
|
||||
static NSError *RKUnprocessableErrorFromResponse(NSHTTPURLResponse *response)
|
||||
{
|
||||
NSCAssert([RKErrorStatusCodes() containsIndex:response.statusCode], @"Expected response status code to be in the 400-599 range, instead got %ld", (long) response.statusCode);
|
||||
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
|
||||
[userInfo setValue:[NSString stringWithFormat:@"Loaded an unprocessable client error response (%ld)", (long) response.statusCode] forKey:NSLocalizedDescriptionKey];
|
||||
[userInfo setValue:[NSString stringWithFormat:@"Loaded an unprocessable error response (%ld)", (long) response.statusCode] forKey:NSLocalizedDescriptionKey];
|
||||
[userInfo setValue:[response URL] forKey:NSURLErrorFailingURLErrorKey];
|
||||
|
||||
return [[NSError alloc] initWithDomain:RKErrorDomain code:NSURLErrorBadServerResponse userInfo:userInfo];
|
||||
@@ -91,17 +102,6 @@ static NSString *RKFailureReasonErrorStringForResponseDescriptorsMismatchWithRes
|
||||
return failureReason;
|
||||
}
|
||||
|
||||
static NSIndexSet *RKErrorStatusCodes()
|
||||
{
|
||||
static NSIndexSet *errorStatusCodes = nil;
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
errorStatusCodes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(400, 200)];
|
||||
});
|
||||
|
||||
return errorStatusCodes;
|
||||
}
|
||||
|
||||
/**
|
||||
A serial dispatch queue used for all deserialization of response bodies
|
||||
*/
|
||||
@@ -226,7 +226,7 @@ static dispatch_queue_t RKResponseMapperSerializationQueue() {
|
||||
|
||||
// If we are an error response and empty, we emit an error that the content is unmappable
|
||||
if (isErrorStatusCode && [self hasEmptyResponse]) {
|
||||
self.error = RKUnprocessableClientErrorFromResponse(self.response);
|
||||
self.error = RKUnprocessableErrorFromResponse(self.response);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -274,7 +274,7 @@ static dispatch_queue_t RKResponseMapperSerializationQueue() {
|
||||
error = RKErrorFromMappingResult(self.mappingResult);
|
||||
} else {
|
||||
// We encountered a client error that we could not map, throw unprocessable error
|
||||
if (! error) error = RKUnprocessableClientErrorFromResponse(self.response);
|
||||
if (! error) error = RKUnprocessableErrorFromResponse(self.response);
|
||||
}
|
||||
self.error = error;
|
||||
return;
|
||||
|
||||
@@ -87,7 +87,7 @@ NSString *RKPathAndQueryStringFromURLRelativeToURL(NSURL *URL, NSURL *baseURL);
|
||||
[mapper start];
|
||||
expect(mapper.error).notTo.beNil();
|
||||
expect(mapper.error.code).to.equal(NSURLErrorBadServerResponse);
|
||||
expect([mapper.error localizedDescription]).to.equal(@"Loaded an unprocessable client error response (422)");
|
||||
expect([mapper.error localizedDescription]).to.equal(@"Loaded an unprocessable error response (422)");
|
||||
}
|
||||
|
||||
// 422, with mappable error payload
|
||||
@@ -118,7 +118,7 @@ NSString *RKPathAndQueryStringFromURLRelativeToURL(NSURL *URL, NSURL *baseURL);
|
||||
[mapper start];
|
||||
expect(mapper.error).notTo.beNil();
|
||||
expect(mapper.error.code).to.equal(NSURLErrorBadServerResponse);
|
||||
expect([mapper.error localizedDescription]).to.equal(@"Loaded an unprocessable client error response (422)");
|
||||
expect([mapper.error localizedDescription]).to.equal(@"Loaded an unprocessable error response (422)");
|
||||
}
|
||||
|
||||
// 422, empty JSON dictionary, no response descriptors
|
||||
@@ -131,7 +131,7 @@ NSString *RKPathAndQueryStringFromURLRelativeToURL(NSURL *URL, NSURL *baseURL);
|
||||
[mapper start];
|
||||
expect(mapper.error).notTo.beNil();
|
||||
expect(mapper.error.code).to.equal(NSURLErrorBadServerResponse);
|
||||
expect([mapper.error localizedDescription]).to.equal(@"Loaded an unprocessable client error response (422)");
|
||||
expect([mapper.error localizedDescription]).to.equal(@"Loaded an unprocessable error response (422)");
|
||||
}
|
||||
|
||||
- (void)testMappingServerErrorToCustomErrorClass
|
||||
|
||||
Reference in New Issue
Block a user