简体   繁体   中英

MappingProvider's errorMapping always fails with: Could not find an object mapping for keyPath: ''

I am experiencing difficulties mapping my API's errors. I am loading requests using blocks like this:

[self.objectManager loadObjectsAtResourcePath:resourcePath usingBlock:^(RKObjectLoader *loader) {
    loader.onDidLoadObjects = ^(NSArray *objects) {
        // ...
    };
    loader.onDidFailLoadWithError = ^(NSError *error) {
        // ...
    };
    loader.onDidFailWithError = ^(NSError *error) {
        // ...
    };
}];

On the server side if, eg, I attempt to authenticate a user with wrong username/password, my API returns and error with the following format:

{ "error":
    {"code":"401",
    "message":"Wrong username etc. etc."}
}

In the object mapping docs I have found the following:

RKErrorMessage

A simple class providing for the mapping of server-side error messages back to NSError objects [...] When an RKObjectManager is initialized, object mappings from the "error" and "errors" keyPaths to instances of RKErrorMessage are registered with the mapping provider. This provides out of the box mapping support for simple error messages. [...]

Which I interpret as the object loader automatically will map any "error" og "errors" keyPath's accordingly, and didFailWithError: (or block) will get called. This is not the situation, though. I get this error every time an error response is loaded from the API:

Adding mapping error: Could not find an object mapping for keyPath: ''

I have tried solutions from this questions and this question , but the result is the same.

I know the json is loaded from my API, I have tracked that in the debugger. Also, if I create an RKObjectMapping and maps it to the "error" keyPath, onDidLoadObjects block is fired with an Error object, but this is not what I intend to do.

Long story short, my goal is that make RestKit fire an onDidFailWithError block whenever an "error" keyPath is returned by my API.

From your question I would assume the problem to be with your server the error response with a 2xx status code.

Other possibilities are either using a version of RK predating the docs you mention, or you might have found a bug :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM