Skip to content

Instantly share code, notes, and snippets.

@ddonovan
Created October 19, 2014 04:10
Show Gist options
  • Save ddonovan/ed80c9ac04fea50f45cf to your computer and use it in GitHub Desktop.
Save ddonovan/ed80c9ac04fea50f45cf to your computer and use it in GitHub Desktop.
sample of how to change AFHTTPSessionManager : GET response failure block updated to extract the error message sent from server and use inside an NSError object or however you might use it.
failure:^(NSURLSessionDataTask *task, NSError *error) {
//convert error.data back to json
NSError* jsonerror;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:error.userInfo[@"com.alamofire.serialization.response.error.data"] //1
options:kNilOptions
error:&jsonerror];
NSLog(@"json response data %@",json);
//make a copy of error.userInfo to allow us to change just the fields we want updated.
NSMutableDictionary *userInfo = [ error.userInfo mutableCopy];
// grab the json error from the server.
userInfo[NSLocalizedDescriptionKey]=json[@"error"][@"message"];
// create a new NSError using most of what the original error contained but replacing our error message.
NSError *betterError = [NSError errorWithDomain:error.domain code:error.code userInfo:userInfo];
NSLog(@"Error %@",error);
NSLog(@"Better Error %@",betterError);
dispatch_async(dispatch_get_main_queue(), ^{
completion(nil, betterError);
});
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment