Permalink
Browse files

[Issue #16] Formalizing AFNetworkingErrorDomain, and constructing err…

…ors accordingly
  • Loading branch information...
1 parent a0c5707 commit d62af91ac97b70a0018bcd6b6c2a372b3b64a1a6 @mattt mattt committed Sep 15, 2011
@@ -22,6 +22,9 @@
#import <Foundation/Foundation.h>
+// Error codes for AFNetworkingErrorDomain correspond to codes in NSURLErrorDomain
+extern NSString * const AFNetworkingErrorDomain;
+
extern NSString * const AFHTTPOperationDidStartNotification;
extern NSString * const AFHTTPOperationDidFinishNotification;
@@ -32,8 +32,10 @@
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);
@@ -65,14 +65,20 @@ + (id)operationWithRequest:(NSURLRequest *)urlRequest
{
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];
}
}
@@ -22,7 +22,7 @@
#import <Foundation/Foundation.h>
-extern NSString * const kAFZlibErrorDomain;
+extern NSString * const AFZlibErrorDomain;
@interface NSData (AFNetworking)
@@ -21,9 +21,11 @@
// THE SOFTWARE.
#import "NSData+AFNetworking.h"
+#import "AFHTTPRequestOperation.h"
+
#import <zlib.h>
-NSString * const kAFZlibErrorDomain = @"com.alamofire.zlib.error";
+NSString * const AFZlibErrorDomain = @"com.alamofire.networking.zlib.error";
static char Base64EncodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
@@ -72,7 +74,7 @@ + (NSData *)dataByTransformingData:(NSData *)data
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 @@ - (NSString *)base64EncodedString {
}
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] : '=';
}

0 comments on commit d62af91

Please sign in to comment.