Follow overriding pattern for error as used in HTTP operation subclasses

This commit is contained in:
Mattt Thompson 2011-10-24 13:32:42 -05:00
parent 30ea7353d4
commit a37cda1032

View file

@ -23,14 +23,14 @@
#import "AFHTTPRequestOperation.h" #import "AFHTTPRequestOperation.h"
@interface AFHTTPRequestOperation () @interface AFHTTPRequestOperation ()
@property (readwrite, nonatomic, retain) NSError *error; @property (readwrite, nonatomic, retain) NSError *HTTPError;
@property (readonly, nonatomic, assign) BOOL hasContent; @property (readonly, nonatomic, assign) BOOL hasContent;
@end @end
@implementation AFHTTPRequestOperation @implementation AFHTTPRequestOperation
@synthesize acceptableStatusCodes = _acceptableStatusCodes; @synthesize acceptableStatusCodes = _acceptableStatusCodes;
@synthesize acceptableContentTypes = _acceptableContentTypes; @synthesize acceptableContentTypes = _acceptableContentTypes;
@synthesize error = _HTTPError; @synthesize HTTPError = _HTTPError;
- (id)initWithRequest:(NSURLRequest *)request { - (id)initWithRequest:(NSURLRequest *)request {
self = [super initWithRequest:request]; self = [super initWithRequest:request];
@ -55,23 +55,27 @@
} }
- (NSError *)error { - (NSError *)error {
if (self.response) { if (self.response && !self.HTTPError) {
if (![self hasAcceptableStatusCode]) { if (![self hasAcceptableStatusCode]) {
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary]; NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
[userInfo setValue:[NSString stringWithFormat:NSLocalizedString(@"Expected status code %@, got %d", nil), self.acceptableStatusCodes, [self.response statusCode]] forKey:NSLocalizedDescriptionKey]; [userInfo setValue:[NSString stringWithFormat:NSLocalizedString(@"Expected status code %@, got %d", nil), self.acceptableStatusCodes, [self.response statusCode]] forKey:NSLocalizedDescriptionKey];
[userInfo setValue:[self.request URL] forKey:NSURLErrorFailingURLErrorKey]; [userInfo setValue:[self.request URL] forKey:NSURLErrorFailingURLErrorKey];
self.error = [[[NSError alloc] initWithDomain:AFNetworkingErrorDomain code:NSURLErrorBadServerResponse userInfo:userInfo] autorelease]; self.HTTPError = [[[NSError alloc] initWithDomain:AFNetworkingErrorDomain code:NSURLErrorBadServerResponse userInfo:userInfo] autorelease];
} else if ([self hasContent] && ![self hasAcceptableContentType]) { // Don't invalidate content type if there is no content } else if ([self hasContent] && ![self hasAcceptableContentType]) { // Don't invalidate content type if there is no content
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary]; NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
[userInfo setValue:[NSString stringWithFormat:NSLocalizedString(@"Expected content type %@, got %@", nil), self.acceptableContentTypes, [self.response MIMEType]] forKey:NSLocalizedDescriptionKey]; [userInfo setValue:[NSString stringWithFormat:NSLocalizedString(@"Expected content type %@, got %@", nil), self.acceptableContentTypes, [self.response MIMEType]] forKey:NSLocalizedDescriptionKey];
[userInfo setValue:[self.request URL] forKey:NSURLErrorFailingURLErrorKey]; [userInfo setValue:[self.request URL] forKey:NSURLErrorFailingURLErrorKey];
self.error = [[[NSError alloc] initWithDomain:AFNetworkingErrorDomain code:NSURLErrorCannotDecodeContentData userInfo:userInfo] autorelease]; self.HTTPError = [[[NSError alloc] initWithDomain:AFNetworkingErrorDomain code:NSURLErrorCannotDecodeContentData userInfo:userInfo] autorelease];
} }
} }
if (_HTTPError) {
return _HTTPError; return _HTTPError;
} else {
return [super error];
}
} }
- (BOOL)hasContent { - (BOOL)hasContent {