From d62af91ac97b70a0018bcd6b6c2a372b3b64a1a6 Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Thu, 15 Sep 2011 12:06:25 -0500 Subject: [PATCH] [Issue #16] Formalizing AFNetworkingErrorDomain, and constructing errors accordingly --- AFNetworking/AFHTTPRequestOperation.h | 3 +++ AFNetworking/AFHTTPRequestOperation.m | 6 ++++-- AFNetworking/AFJSONRequestOperation.m | 16 +++++++++++----- AFNetworking/NSData+AFNetworking.h | 2 +- AFNetworking/NSData+AFNetworking.m | 10 ++++++---- 5 files changed, 25 insertions(+), 12 deletions(-) diff --git a/AFNetworking/AFHTTPRequestOperation.h b/AFNetworking/AFHTTPRequestOperation.h index daee496..55c7a26 100644 --- a/AFNetworking/AFHTTPRequestOperation.h +++ b/AFNetworking/AFHTTPRequestOperation.h @@ -22,6 +22,9 @@ #import +// Error codes for AFNetworkingErrorDomain correspond to codes in NSURLErrorDomain +extern NSString * const AFNetworkingErrorDomain; + extern NSString * const AFHTTPOperationDidStartNotification; extern NSString * const AFHTTPOperationDidFinishNotification; diff --git a/AFNetworking/AFHTTPRequestOperation.m b/AFNetworking/AFHTTPRequestOperation.m index 6cff354..275aba0 100644 --- a/AFNetworking/AFHTTPRequestOperation.m +++ b/AFNetworking/AFHTTPRequestOperation.m @@ -32,8 +32,10 @@ typedef enum { AFHTTPOperationCancelledState = 4, } AFHTTPOperationState; -NSString * const AFHTTPOperationDidStartNotification = @"com.alamofire.http-operation.start"; -NSString * const AFHTTPOperationDidFinishNotification = @"com.alamofire.http-operation.finish"; +NSString * const AFNetworkingErrorDomain = @"com.alamofire.networking.error"; + +NSString * const AFHTTPOperationDidStartNotification = @"com.alamofire.networking.http-operation.start"; +NSString * const AFHTTPOperationDidFinishNotification = @"com.alamofire.networking.http-operation.finish"; typedef void (^AFHTTPRequestOperationProgressBlock)(NSUInteger totalBytes, NSUInteger totalBytesExpected); typedef void (^AFHTTPRequestOperationCompletionBlock)(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error); diff --git a/AFNetworking/AFJSONRequestOperation.m b/AFNetworking/AFJSONRequestOperation.m index 687e599..0baabd8 100644 --- a/AFNetworking/AFJSONRequestOperation.m +++ b/AFNetworking/AFJSONRequestOperation.m @@ -65,14 +65,20 @@ static dispatch_queue_t json_request_operation_processing_queue() { { return [self operationWithRequest:urlRequest completion:^(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error) { if (!error) { - BOOL statusCodeAcceptable = [acceptableStatusCodes containsIndex:[response statusCode]]; - BOOL contentTypeAcceptable = [acceptableContentTypes containsObject:[response MIMEType]]; - if (!statusCodeAcceptable || !contentTypeAcceptable) { + if (![acceptableStatusCodes containsIndex:[response statusCode]]) { NSMutableDictionary *userInfo = [NSMutableDictionary dictionary]; - [userInfo setValue:[NSHTTPURLResponse localizedStringForStatusCode:[response statusCode]] forKey:NSLocalizedDescriptionKey]; + [userInfo setValue:[NSString stringWithFormat:NSLocalizedString(@"Expected status code %@, got %d", nil), acceptableStatusCodes, [response statusCode]] forKey:NSLocalizedDescriptionKey]; [userInfo setValue:[request URL] forKey:NSURLErrorFailingURLErrorKey]; - error = [[[NSError alloc] initWithDomain:NSURLErrorDomain code:[response statusCode] userInfo:userInfo] autorelease]; + error = [[[NSError alloc] initWithDomain:AFNetworkingErrorDomain code:NSURLErrorBadServerResponse userInfo:userInfo] autorelease]; + } + + if (![acceptableContentTypes containsObject:[response MIMEType]]) { + NSMutableDictionary *userInfo = [NSMutableDictionary dictionary]; + [userInfo setValue:[NSString stringWithFormat:NSLocalizedString(@"Expected content type %@, got %@", nil), acceptableContentTypes, [response MIMEType]] forKey:NSLocalizedDescriptionKey]; + [userInfo setValue:[request URL] forKey:NSURLErrorFailingURLErrorKey]; + + error = [[[NSError alloc] initWithDomain:AFNetworkingErrorDomain code:NSURLErrorCannotDecodeContentData userInfo:userInfo] autorelease]; } } diff --git a/AFNetworking/NSData+AFNetworking.h b/AFNetworking/NSData+AFNetworking.h index 4fe6c36..36e0fbe 100644 --- a/AFNetworking/NSData+AFNetworking.h +++ b/AFNetworking/NSData+AFNetworking.h @@ -22,7 +22,7 @@ #import -extern NSString * const kAFZlibErrorDomain; +extern NSString * const AFZlibErrorDomain; @interface NSData (AFNetworking) diff --git a/AFNetworking/NSData+AFNetworking.m b/AFNetworking/NSData+AFNetworking.m index 9d2fad2..13ba284 100644 --- a/AFNetworking/NSData+AFNetworking.m +++ b/AFNetworking/NSData+AFNetworking.m @@ -21,9 +21,11 @@ // THE SOFTWARE. #import "NSData+AFNetworking.h" +#import "AFHTTPRequestOperation.h" + #import -NSString * const kAFZlibErrorDomain = @"com.alamofire.zlib.error"; +NSString * const AFZlibErrorDomain = @"com.alamofire.networking.zlib.error"; static char Base64EncodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; @@ -72,7 +74,7 @@ typedef enum { break; } else if (status != Z_OK) { if (error) { - *error = [NSError errorWithDomain:kAFZlibErrorDomain code:status userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Compression of data failed with code %hi", status] forKey:NSLocalizedDescriptionKey]]; + *error = [NSError errorWithDomain:AFZlibErrorDomain code:status userInfo:nil]; } return nil; @@ -108,8 +110,8 @@ typedef enum { } NSInteger idx = (i / 3) * 4; - output[idx + 0] = Base64EncodingTable[(value >> 18) & 0x3F]; - output[idx + 1] = Base64EncodingTable[(value >> 12) & 0x3F]; + output[idx + 0] = Base64EncodingTable[(value >> 18) & 0x3F]; + output[idx + 1] = Base64EncodingTable[(value >> 12) & 0x3F]; output[idx + 2] = (i + 1) < length ? Base64EncodingTable[(value >> 6) & 0x3F] : '='; output[idx + 3] = (i + 2) < length ? Base64EncodingTable[(value >> 0) & 0x3F] : '='; }