Created
October 19, 2014 04:10
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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