How do I setup RestKit 0.10.0 to recognize 'error' and 'errors' messages?

寵の児 提交于 2020-01-04 11:38:10

问题


Back before 0.10.0 both 'error' and 'errors' messages 'just worked' with RestKit. I see that in 0.10.0 you can set the rootKeyPath for the error mapping.

    objectManager.mappingProvider.errorMapping.rootKeyPath = @"errors"; 

However I can only set it to error xor errors. To me this seems like a regression. What am I missing?


回答1:


This is what I have to manage errors:

#import <RestKit/RKErrorMessage.h>

...

//configure error manager
RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];
[errorMapping mapKeyPath:@"message" toAttribute:@"errorMessage"];
[objectManager.mappingProvider setErrorMapping:errorMapping];



回答2:


In 0.10.0 RestKit added the error context which only tries to map the response to an error mapping if the status code is 4xx or 5xx but they only allow for one top level key path (as far as I know). Thus I couldn't map both 'errors' and 'errors'. I tried to fix this without modifying restkit but I had to modify one line which you can see in my branch https://github.com/stevemoser/RestKit/tree/fix-errors-context Then when I setup my error mapping it looks like this:

#import <RestKit/RKErrorMessage.h>
#import <RestKit/RKObjectMappingProvider+Contexts.h>

[objectManager.mappingProvider setErrorMapping:nil];
[objectManager.mappingProvider setValue:[NSMutableDictionary dictionary] forContext:RKObjectMappingProviderContextErrors];
RKObjectMapping* errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];
[errorMapping mapKeyPath:@"" toAttribute:@"errorMessage"];
[objectManager.mappingProvider setMapping:errorMapping forKeyPath:@"error" context:RKObjectMappingProviderContextErrors];
[objectManager.mappingProvider setMapping:errorMapping forKeyPath:@"errors" context:RKObjectMappingProviderContextErrors];  


来源:https://stackoverflow.com/questions/10851239/how-do-i-setup-restkit-0-10-0-to-recognize-error-and-errors-messages

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