Search code examples
iosobjective-cresterror-handlingrestkit

RestKit PUT request with error mapping


Im making an app that communicates with a rails based backend server. I already have all the server calls ready and working via RESTKit, but I am having problems with creating error mappings for my update calls.

My response descriptors for one of my classes

RKResponseDescriptor *descriptor = [RKResponseDescriptor responseDescriptorWithMapping:[Mappings liveViewMapping]
                                                                                method:RKRequestMethodAny
                                                                           pathPattern:nil
                                                                               keyPath:@"event_enriched"
                                                                           statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[self addResponseDescriptor:descriptor];

RKResponseDescriptor *errorDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[Mappings errorMapping]
                                                                                     method:RKRequestMethodAny
                                                                                pathPattern:nil
                                                                                    keyPath:@"error"
                                                                                statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError)];
[self addResponseDescriptor:errorDescriptor];

Edit: I've also tried it with keyPath "errors" and 'nil' .. same results

My error mapping is quite simple:

+ (RKObjectMapping *)errorCollectionMapping {
    RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[ErrorCollection class]];

    NSDictionary *mappingDictionary = @{@"error"    : @"message",
                                        @"errors"   : @"messages",
                                        };

    [errorMapping addAttributeMappingsFromDictionary:mappingDictionary];

    return errorMapping;
}

This is how I am trying to update my Book object

[self putObject:book
           path:API_BOOK
     parameters:nil
        success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
            if (success) {
                Book *book = [mappingResult.dictionary objectForKey:@"book"];
                success(book);
            }
     } failure:^(RKObjectRequestOperation *operation, NSError *error) {
         if (failure) failure(operation, error);
     }
];

But when I get a server error, which should be handled by my response descriptor I get the following error:

Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No mappable object representations were found at the key paths searched." UserInfo=0x7f935d07c7a0 {NSLocalizedDescription=No mappable object representations were found at the key paths searched., NSLocalizedFailureReason=The mapping operation was unable to find any nested object representations at the key paths searched: book The representation inputted to the mapper was found to contain nested object representations at the following key paths: error, error_description This likely indicates that you have misconfigured the key paths for your mappings., keyPath=null, DetailedErrors=( )}

What am I doing wrong? I already have some GET requests, that have multiple mappings (non of them is an error mapping yet) which works fine, but can't "replicate" the same behavior with error mapping

oh.. I am using restkit-0.24 :)

edit: the error responses coming back from my rails server are in this form:

{"errors": ["error1", "error2" ... ] } 

or

{"error": "error message" }

Solution

  • i feel so stupid right now ...

    The server responses had code 200 and not the 400-499 range that RKStatusCodeClassClientError predicts.