Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Commit

Permalink
Follow overriding pattern for error as used in HTTP operation subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
mattt committed Oct 24, 2011
1 parent 30ea735 commit a37cda1
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions AFNetworking/AFHTTPRequestOperation.m
Expand Up @@ -23,14 +23,14 @@
#import "AFHTTPRequestOperation.h"

@interface AFHTTPRequestOperation ()
@property (readwrite, nonatomic, retain) NSError *error;
@property (readwrite, nonatomic, retain) NSError *HTTPError;
@property (readonly, nonatomic, assign) BOOL hasContent;
@end

@implementation AFHTTPRequestOperation
@synthesize acceptableStatusCodes = _acceptableStatusCodes;
@synthesize acceptableContentTypes = _acceptableContentTypes;
@synthesize error = _HTTPError;
@synthesize HTTPError = _HTTPError;

- (id)initWithRequest:(NSURLRequest *)request {
self = [super initWithRequest:request];
Expand All @@ -55,23 +55,27 @@ - (NSHTTPURLResponse *)response {
}

- (NSError *)error {
if (self.response) {
if (self.response && !self.HTTPError) {
if (![self hasAcceptableStatusCode]) {
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
[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];

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
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
[userInfo setValue:[NSString stringWithFormat:NSLocalizedString(@"Expected content type %@, got %@", nil), self.acceptableContentTypes, [self.response MIMEType]] forKey:NSLocalizedDescriptionKey];
[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];
}
}

return _HTTPError;
if (_HTTPError) {
return _HTTPError;
} else {
return [super error];
}
}

- (BOOL)hasContent {
Expand Down

0 comments on commit a37cda1

Please sign in to comment.