问题
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