问题
I am using Restkit .20 and trying to get the error object returned in a failure block. I have tried both below and both return (null) for the error object.
RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];
[errorMapping addPropertyMapping:
[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"errorMessage"]];
RKResponseDescriptor *errorDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:errorMapping
pathPattern:nil
keyPath:@"error" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError)];
[self.objectManager addResponseDescriptorsFromArray:@[errorDescriptor]];
AND
RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];
[errorMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"error" toKeyPath:@"errorMessage"]];
[self.objectManager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:errorMapping pathPattern:nil keyPath:@"error" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError)]];
This is in my failure block which prints out "errorMessage: (null)"
failure:^(RKObjectRequestOperation *operation, NSError *error)
{
NSLog(@"errorMessage: %@", [[error userInfo] objectForKey:RKObjectMapperErrorObjectsKey]);
}
Here is what is return from in the JSON from the server.
{
"status": "FAIL",
"user_errors": null,
"error": "Credit card type is not accepted by this merchant account.",
"user": null
}
回答1:
An HTTP status code 201 is a success code, not an error code so your error mapping will not be tried (because of statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError)
).
So, update the server to send an appropriate status code. If you update the response descriptor to check 'successful' responses for errors I think it will work.
来源:https://stackoverflow.com/questions/19736408/restkit-failure-getting-access-to-the-json-returned