When the response status code is not 2xx the failure block will be called, you can catch your custom error there, switching on operation.HTTPRequestOperation.response.statusCode
If you want to handle errors with RestKit, in a more generic way, you can consider to add to your RKObjectManager a response descriptor for all 4xx and 5xx errors - in this example i'll map error with this body structure
{"errorCode":"996","errorMessage":"This is my custom error message"}
Configure your 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
And then in the failure block of your requests you can switch on the status code and handle the error:
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
}
}