AFNetworking/AFNetworking — Issue #166: feature request: better handling of server return code in JSON
Author: marcolinux
Created: 2012-01-09 09:20:08+00:00
State: open

Hello,
currently AFJSONRequestOperation in case of error, it is returning a NSError (coming from the super AFHTTPOperation) which is containing a localised string not in relation neither with the original error code, neither with the original server answer.

It would be nice if (at least) the JSON answer (if any) would be returned within the NSError description.
I modified a bit the code (just a quick proposal, surely need to be polished)

--- a/AFNetworking/AFJSONRequestOperation.m
+++ b/AFNetworking/AFJSONRequestOperation.m
@@ -126,7 +126,9 @@ static dispatch_queue_t json_request_operation_processing_queue() {
if (self.error) {
if (failure) {
dispatch_async(dispatch_get_main_queue(), ^(void) {
- failure(self, self.error);
+ NSDictionary *userInfo = self.responseJSON;
+ NSError *er = [[[NSError alloc] initWithDomain:AFNetworkingErrorDomain code:[self.response statusCode] userInfo:userInfo] autorelease];
+ failure(self, er);
});

As for return error code, this question is more theoretical, ie should we just return the HTTP error code, or stick with the NSURLError list?
For an HTTP answer -> 401 Unauthorized eventually I would expect a NSURLErrorUserAuthenticationRequired, but for other in the range ~400 there might not be a correspondent NSURLError.