Override error implementation to correct the NSLocalizedDescription key for RKHTTPRequestOperation objects. fixes #1070

This commit is contained in:
Blake Watters
2012-12-11 23:12:20 -05:00
parent 138bbddfcd
commit 69c65ef8ab
6 changed files with 114 additions and 4 deletions

View File

@@ -28,6 +28,8 @@
#undef RKLogComponent
#define RKLogComponent RKlcl_cRestKitNetwork
NSString *RKStringFromIndexSet(NSIndexSet *indexSet); // Defined in RKResponseDescriptor.m
static BOOL RKLogIsStringBlank(NSString *string)
{
return ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0);
@@ -163,10 +165,13 @@ static NSString *RKStringDescribingStream(NSStream *stream)
@end
@interface AFURLConnectionOperation () <NSURLConnectionDelegate, NSURLConnectionDataDelegate>
@property (readwrite, nonatomic, strong) NSError *HTTPError;
@end
@implementation RKHTTPRequestOperation
@dynamic HTTPError;
- (BOOL)hasAcceptableStatusCode
{
return self.acceptableStatusCodes ? [self.acceptableStatusCodes containsIndex:[self.response statusCode]] : [super hasAcceptableStatusCode];
@@ -177,6 +182,32 @@ static NSString *RKStringDescribingStream(NSStream *stream)
return self.acceptableContentTypes ? RKMIMETypeInSet([self.response MIMEType], self.acceptableContentTypes) : [super hasAcceptableContentType];
}
- (NSError *)error
{
// The first we are invoked, we need to mutate the HTTP error to correct the Content Types and Status Codes returned
if (self.response && !self.HTTPError) {
NSError *error = [super error];
if ([error.domain isEqualToString:AFNetworkingErrorDomain]) {
if (![self hasAcceptableStatusCode] || ![self hasAcceptableContentType]) {
NSMutableDictionary *userInfo = [error.userInfo mutableCopy];
if (error.code == NSURLErrorBadServerResponse) {
// Replace the NSLocalizedDescriptionKey
NSUInteger statusCode = ([self.response isKindOfClass:[NSHTTPURLResponse class]]) ? (NSUInteger)[self.response statusCode] : 200;
[userInfo setValue:[NSString stringWithFormat:NSLocalizedString(@"Expected status code in (%@), got %d", nil), RKStringFromIndexSet([self acceptableStatusCodes]), statusCode] forKey:NSLocalizedDescriptionKey];
self.HTTPError = [[NSError alloc] initWithDomain:AFNetworkingErrorDomain code:NSURLErrorBadServerResponse userInfo:userInfo];
} else if (error.code == NSURLErrorCannotDecodeContentData) {
// Because we have shifted the Acceptable Content Types and Status Codes
[userInfo setValue:[NSString stringWithFormat:NSLocalizedString(@"Expected content type %@, got %@", nil), [self acceptableContentTypes], [self.response MIMEType]] forKey:NSLocalizedDescriptionKey];
self.HTTPError = [[NSError alloc] initWithDomain:AFNetworkingErrorDomain code:NSURLErrorCannotDecodeContentData userInfo:userInfo];
}
}
}
}
return [super error];
}
- (BOOL)wasNotModified
{
return [(NSString *)[[self.response allHeaderFields] objectForKey:@"Status"] isEqualToString:@"304 Not Modified"];