我正在尝试将403 forbidden respone映射到自定义对象。我使用响应代码注册了一个映射,如下所示:
RKResponseDescriptor* tacResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:termsAndConditionMapping pathPattern:@"user" keyPath:nil statusCodes:[NSIndexSet indexSetWithIndex:403]];
[objectManager addResponseDescriptor:tacResponseDescriptor];
但是当从服务器发送403
响应时,我只是从RestKit获取一个'Non-successful status code encountered: performing mapping with nil target object
'日志,并且执行失败块时操作的mappingResult
属性为{{ 1}}。我在这做错了什么?我需要将特定路径的一个特定错误情况映射到特定对象。
祝你好运, 迈克尔
答案 0 :(得分:1)
我不完全确定你的意思,但是如果你最终进入故障区域,则没有mappingResult属性。但是可以在operation
属性
[[[operation.error userInfo] objectForKey: RKObjectMapperErrorObjectsKey] firstObject];
答案 1 :(得分:0)
当响应状态代码不是2xx时,将调用失败块,您可以在那里捕获自定义错误,启用operation.HTTPRequestOperation.response.statusCode
如果你想用更通用的方式处理RestKit的错误,你可以考虑在你的RKObjectManager中为所有4xx和5xx错误添加一个响应描述符 - 在这个例子中,我将使用这个主体结构映射错误
{"errorCode":"996","errorMessage":"This is my custom error message"}
配置您的RKObjectManager:
RKObjectMapping* errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];
[errorMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"errorMessage" toKeyPath:@"errorMessage"]];
[errorMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"userInfo"]];
NSIndexSet *clientErrorStatusCode = RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError); // Any response in the 4xx
NSIndexSet *serverErrorStatusCode = RKStatusCodeIndexSetForClass(RKStatusCodeClassServerError); // Any response in the 5xx
RKResponseDescriptor *clientErrorDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:errorMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"" statusCodes:clientErrorStatusCode];
RKResponseDescriptor *serverErrorDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:errorMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"" statusCodes:serverErrorStatusCode];
//... Remember to add response descriptors to your object manager
然后在您的请求的失败块中,您可以打开状态代码并处理错误:
failure:^(RKObjectRequestOperation *operation, NSError *error) {
RKErrorMessage *errorMessage = [[error.userInfo objectForKey:RKObjectMapperErrorObjectsKey] firstObject];
NSString *errorCode = [errorMessage.userInfo objectForKey:@"errorCode"];
if (operation.HTTPRequestOperation.response.statusCode == 304){
//Handle your 304 error
}
}