Restkit failure: getting access to the json returned

孤者浪人 提交于 2019-12-13 04:33:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!