[Issue #472] Defining error keys for failing request and response to be included in NSError created in AFHTTPRequestOperation
Refactoring AFHTTPRequestOperation error handling code
This commit is contained in:
parent
730c35926a
commit
ed94ddf7b0
3 changed files with 27 additions and 12 deletions
|
|
@ -126,21 +126,24 @@ static NSString * AFStringFromIndexSet(NSIndexSet *indexSet) {
|
||||||
|
|
||||||
- (NSError *)error {
|
- (NSError *)error {
|
||||||
if (self.response && !self.HTTPError) {
|
if (self.response && !self.HTTPError) {
|
||||||
if (![self hasAcceptableStatusCode]) {
|
if (![self hasAcceptableStatusCode] || ![self hasAcceptableContentType]) {
|
||||||
NSUInteger statusCode = ([self.response isKindOfClass:[NSHTTPURLResponse class]]) ? (NSUInteger)[self.response statusCode] : 200;
|
|
||||||
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
|
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
|
||||||
[userInfo setValue:[NSString stringWithFormat:NSLocalizedString(@"Expected status code in (%@), got %d", nil), AFStringFromIndexSet([[self class] acceptableStatusCodes]), statusCode] forKey:NSLocalizedDescriptionKey];
|
|
||||||
[userInfo setValue:self.responseString forKey:NSLocalizedRecoverySuggestionErrorKey];
|
[userInfo setValue:self.responseString forKey:NSLocalizedRecoverySuggestionErrorKey];
|
||||||
[userInfo setValue:[self.request URL] forKey:NSURLErrorFailingURLErrorKey];
|
[userInfo setValue:[self.request URL] forKey:NSURLErrorFailingURLErrorKey];
|
||||||
|
[userInfo setValue:self.request forKey:AFNetworkingOperationFailingURLRequestErrorKey];
|
||||||
|
[userInfo setValue:self.response forKey:AFNetworkingOperationFailingURLResponseErrorKey];
|
||||||
|
|
||||||
self.HTTPError = [[[NSError alloc] initWithDomain:AFNetworkingErrorDomain code:NSURLErrorBadServerResponse userInfo:userInfo] autorelease];
|
if (![self hasAcceptableStatusCode]) {
|
||||||
} else if ([self.responseData length] > 0 && ![self hasAcceptableContentType]) { // Don't invalidate content type if there is no content
|
NSUInteger statusCode = ([self.response isKindOfClass:[NSHTTPURLResponse class]]) ? (NSUInteger)[self.response statusCode] : 200;
|
||||||
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
|
[userInfo setValue:[NSString stringWithFormat:NSLocalizedString(@"Expected status code in (%@), got %d", nil), AFStringFromIndexSet([[self class] acceptableStatusCodes]), statusCode] forKey:NSLocalizedDescriptionKey];
|
||||||
[userInfo setValue:[NSString stringWithFormat:NSLocalizedString(@"Expected content type %@, got %@", nil), [[self class] acceptableContentTypes], [self.response MIMEType]] forKey:NSLocalizedDescriptionKey];
|
self.HTTPError = [[[NSError alloc] initWithDomain:AFNetworkingErrorDomain code:NSURLErrorBadServerResponse userInfo:userInfo] autorelease];
|
||||||
[userInfo setValue:self.responseString forKey:NSLocalizedRecoverySuggestionErrorKey];
|
} else if (![self hasAcceptableContentType]) {
|
||||||
[userInfo setValue:[self.request URL] forKey:NSURLErrorFailingURLErrorKey];
|
// Don't invalidate content type if there is no content
|
||||||
|
if ([self.responseData length] > 0) {
|
||||||
self.HTTPError = [[[NSError alloc] initWithDomain:AFNetworkingErrorDomain code:NSURLErrorCannotDecodeContentData userInfo:userInfo] autorelease];
|
[userInfo setValue:[NSString stringWithFormat:NSLocalizedString(@"Expected content type %@, got %@", nil), [[self class] acceptableContentTypes], [self.response MIMEType]] forKey:NSLocalizedDescriptionKey];
|
||||||
|
self.HTTPError = [[[NSError alloc] initWithDomain:AFNetworkingErrorDomain code:NSURLErrorCannotDecodeContentData userInfo:userInfo] autorelease];
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,16 @@
|
||||||
*/
|
*/
|
||||||
extern NSString * const AFNetworkingErrorDomain;
|
extern NSString * const AFNetworkingErrorDomain;
|
||||||
|
|
||||||
|
/**
|
||||||
|
The corresponding value is an `NSURLRequest` containing the request of the operation associated with an error. This key is only present in the `AFNetworkingErrorDomain`.
|
||||||
|
*/
|
||||||
|
extern NSString * const AFNetworkingOperationFailingURLRequestErrorKey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
The corresponding value is an `NSURLResponse` containing the response of the operation associated with an error. This key is only present in the `AFNetworkingErrorDomain`.
|
||||||
|
*/
|
||||||
|
extern NSString * const AFNetworkingOperationFailingURLResponseErrorKey;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Posted when an operation begins executing.
|
Posted when an operation begins executing.
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,9 @@ typedef id AFBackgroundTaskIdentifier;
|
||||||
|
|
||||||
static NSString * const kAFNetworkingLockName = @"com.alamofire.networking.operation.lock";
|
static NSString * const kAFNetworkingLockName = @"com.alamofire.networking.operation.lock";
|
||||||
|
|
||||||
NSString * const AFNetworkingErrorDomain = @"com.alamofire.networking.error";
|
NSString * const AFNetworkingErrorDomain = @"AFNetworkingErrorDomain";
|
||||||
|
NSString * const AFNetworkingOperationFailingURLRequestErrorKey = @"AFNetworkingOperationFailingURLRequestErrorKey";
|
||||||
|
NSString * const AFNetworkingOperationFailingURLResponseErrorKey = @"AFNetworkingOperationFailingURLResponseErrorKey";
|
||||||
|
|
||||||
NSString * const AFNetworkingOperationDidStartNotification = @"com.alamofire.networking.operation.start";
|
NSString * const AFNetworkingOperationDidStartNotification = @"com.alamofire.networking.operation.start";
|
||||||
NSString * const AFNetworkingOperationDidFinishNotification = @"com.alamofire.networking.operation.finish";
|
NSString * const AFNetworkingOperationDidFinishNotification = @"com.alamofire.networking.operation.finish";
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue