14

I have used many hours on how to solve this issue. Im using Restkit 0.9.3 with Object Mapping 2.0. All data is in JSON. I can make GET, POST, PUT and DELETE operations correctly, it's the response body I dont catch and map corretly..

So my problem is that my restful api is returning errors when something goes wrong. I want to map those errors with restkit, fx this error is returned:

{ "code": "401", "message": "Unauthorized" }

How do I map this json correct? I have tried lots of things and could use some guideness - or please give an example of this.

4 Answers 4

29

For RestKit v0.20

assuming your HTTP body is:

{"error": "..."}

you create an the mapping and descriptor:

RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];

[errorMapping addPropertyMapping: [RKAttributeMapping attributeMappingFromKeyPath:@"error" toKeyPath:@"errorMessage"]];
RKResponseDescriptor *errorResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:errorMapping pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError)];
[objectManager addResponseDescriptor:errorResponseDescriptor];

and then you can access it from your failure block:

failure:^(RKObjectRequestOperation *operation, NSError *error) {
                NSLog(@"errorMessage: %@", [[error userInfo] objectForKey:RKObjectMapperErrorObjectsKey]);
            }

This uses the built-in RKErrorMessage class, though you can create your own custom class with additional fields.

3
  • This doesn't work. The error comesback null. NSLOG shows errorMessage: (null) stackoverflow.com/questions/19736408/…
    – jdog
    Nov 1, 2013 at 22:46
  • 1
    Works fine for me and others. Are you using the correct mapping for your class?
    – Brenden
    Nov 4, 2013 at 17:01
  • 1
    @jgervin would you care to share your JSON? I'd like that -1 turned to a +1 :]
    – Brenden
    Nov 7, 2013 at 18:43
10

For version 0.10.0 this response error can be mapped as follows:

RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];
[errorMapping mapKeyPath:@"message" toAttribute:@"errorMessage"];
[[[RKObjectManager sharedManager] mappingProvider] setErrorMapping:errorMapping];

Requests that return an error will call the following delegate method:

- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error {
    NSArray *errorMessages = [[error userInfo] objectForKey:RKObjectMapperErrorObjectsKey];
    RKErrorMessage *errorMessage = [errorMessages objectAtIndex:0]; // First and only object in your case.
    NSString *message = [errorMessage errorMessage];
    NSInteger code = [[objectLoader response] statusCode];
    NSLog(@"ERROR: [%d] %@", code, message); // => ERROR: [401] Unauthorized
}
3
  • 1
    Any idea how to do this in v.20? Dec 18, 2012 at 0:17
  • 1
    see my answer below for v0.20
    – Brenden
    Sep 26, 2013 at 20:43
  • What if the JSON response is in this format: { "message": "Unauthorized", "critical":true } ? Where can I get the value for the key 'critical'? Apr 13, 2015 at 6:39
5

Using Restkit v0.2x you can map all JSON attributes you want to the already existing RKErrorMessage.userInfo Dictionary property in this [Swift] way:

let errorMapping = RKObjectMapping(forClass: RKErrorMessage.self);
errorMapping.addPropertyMapping(RKAttributeMapping(fromKeyPath: nil, toKeyPath: "userInfo"));

let errorResponseDescriptor = RKResponseDescriptor(
    mapping: errorMapping,
    method: RKRequestMethod.Any,
    pathPattern: nil,
    keyPath: "error", //or nil, according to your json response
    statusCodes: RKStatusCodeIndexSetForClass(UInt(RKStatusCodeClassClientError)))
);

So, you can map an error JSON response like this one:

{
    "error": {
        "message": "Error message",
        "cause": "...",
        "code": "my_error_code",
        "url": "..."
        ...
    }
}

And retrieve the RKErrorMessage with all attributes, in a failure closure, as follows:

failure: { (operation, error) -> Void in
    if let errorMessage = error.userInfo?[RKObjectMapperErrorObjectsKey]?.firstObject as? RKErrorMessage{
        let message = errorMessage.userInfo["message"] as! String;
        let code = errorMessage.userInfo["code"] as! String;
        ...
    }
}

I hope this can be helpful to someone!

1
  • Instead of statusCodes: RKStatusCodeIndexSetForClass(UInt(RKStatusCodeClassClientError))you could also use statusCodes: RKStatusCodeIndexSetForClass(.ClientError)
    – anka
    Aug 3, 2016 at 12:06
0

if an object is returned your delegate should call this method:

- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {}

in this method you should call an instance of your "error" class and map 'code' and 'message' as necessary.

An easier way to handle errors though would be to use:

- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {

if(objectLoader.response.statusCode == 401)
{ ...

And show the necessary error message in that method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.