How do I map custom error keys in Restkit? - ios

When a user signs up successfully on our server, it responds with a 200 status code and a JSON payload like this:
{
"error": null,
"result": {
"auth": {
"created_utc": 1420740197,
"device_token": "rQZJddrbD5tyEpznb8bVKeGlHqRNGyvOgDR;tQJBkpkfAXO6DQ4lNiG17lzu6IDc0hVBfR3RrN9o0txRQIYAa6fnf5d9LNaSRDMk9LrplgkITuMC37v;;;rvG35CJvV7dWZ5TQVYUWeHwAABvKvzTRpSDw5Qg9jQrmiUHLZptegFY=76421420740197"
},
"display_name": "a",
"email": "a#a.com",
"user_id": 7642,
"username": "a"
}
}
But if a#a.com tries to sign up again, it responds with a 400 status code and a JSON payload like this:
{
"error": {
"code": 805,
"message": "there is another user with that username"
},
"result": null
}
I tried mapping the error, so that when Restkit returns an error, I get the message as well as the code. These are the ways I tried to do that:
RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];
[errorMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:#"error.message" toKeyPath:#"errorMessage"]];
RKResponseDescriptor *errorDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:errorMapping method:RKRequestMethodAny pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError)];
[self.objectManager addResponseDescriptor:errorDescriptor];
This one obviously only gets the message part of the error to return with this output:
Error Domain=org.restkit.RestKit.ErrorDomain Code=1004 "there is another user with that username" UserInfo=0x7feefcf8a730 {RKObjectMapperErrorObjectsKey=(
"there is another user with that username"
), NSLocalizedDescription=there is another user with that username}
So then I tried making a subclass of RKErrorMessage:
#import "RKErrorMessage.h"
#interface TAG_RKErrorMessage : RKErrorMessage
#property (strong, nonatomic) NSNumber *errorCode;
#end
And changed the mapping to this:
RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[TAG_RKErrorMessage class]];
[errorMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:#"error.message" toKeyPath:#"errorMessage"]];
[errorMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:#"error.code" toKeyPath:#"errorCode"]];
and that resulted in the exact same output:
Error Domain=org.restkit.RestKit.ErrorDomain Code=1004 "there is another user with that username" UserInfo=0x7fa16c627ce0 {RKObjectMapperErrorObjectsKey=(
"there is another user with that username"
), NSLocalizedDescription=there is another user with that username}
So lastly I tried this mapping to at least try to get the dictionary show up as the RKObjectMapperErrorObjectsKey of the NSError's userInfo:
RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];
[errorMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:#"error" toKeyPath:#"errorMessage"]];
And that resulted in this output:
Error Domain=org.restkit.RestKit.ErrorDomain Code=1004 "<null>" UserInfo=0x7ff8abe678e0 {RKObjectMapperErrorObjectsKey=(
(null)
), NSLocalizedDescription=<null>}
Now I'm stuck at this point. How can I map the keys of my server's error response so that I can get access to the code that is returned as well as the message as two separate values?

You've likely mapped your TAG_RKErrorMessage instance correctly. Have you attempted to extract it from the NSError you've been given? You can use the RKObjectMapperErrorObjectsKey as a key on the NSError's userInfo dictionary to get an array of all mapped error messages:
NSArray* objectMapperErrorObjectsArray = [error.userInfo objectForKey:RKObjectMapperErrorObjectsKey];
You can then loop through that array to check each error for conditions you want to respond to, for example, if you wanted to respond to a 403 response code with the error message "Invalid token", you could do:
NSArray* objectMapperErrorObjectsArray = [error.userInfo objectForKey:RKObjectMapperErrorObjectsKey];
for (RKErrorMessage* objectMapperErrorObject in objectMapperErrorObjectsArray)
{
if ([objectMapperErrorObject.errorMessage isEqual:#"Invalid token"])
{
if (operation.HTTPRequestOperation.response.statusCode == 403)
{
//Code to handle a 403 response status code when error message is "Invalid token".
}
}
}

Just a hint: if you want to use the built-in RKErrorMessage without creating any subclass, you can map all error JSON attributes to RKErrorMessage.userInfo in this way:
Objective-C:
RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];
[errorMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:#"userInfo"]];
RKResponseDescriptor *errorDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:errorMapping method:RKRequestMethodAny pathPattern:nil keyPath:"error" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError)];
Swift:
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",
statusCodes: RKStatusCodeIndexSetForClass(UInt(RKStatusCodeClassClientError)))
);

Related

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" }
i feel so stupid right now ...
The server responses had code 200 and not the 400-499 range that RKStatusCodeClassClientError predicts.

RestKit - Error Domain Code 1001

OK, so I've reviewed almost every single one of the other questions on this site, to no avail.
Here's my JSON that comes back from a REST service:
{
"errors" : {};
"result" : {
"messagebody" : "Hello!";
"timestamp" : "2014-08-21T04:12:28.4689099+00:00";
};
"success" : {};
}
I am trying to pull out the result object via RestKit v0.20.3
Here's a block of my code where it gets configured/executed:
- (void) configureRestKit
{
NSURL *cminstance = [NSURL URLWithString:#"http://<domain>"];
AFHTTPClient *cmclient = [[AFHTTPClient alloc] initWithBaseURL:cminstance];
RKObjectManager *objmgr = [[RKObjectManager alloc] initWithHTTPClient:cmclient];
RKObjectMapping *messageMap = [RKObjectMapping mappingForClass:[Message class]];
[messageMap addAttributeMappingsFromDictionary:#{ #"messagebody" : #"messagebody", #"timestamp": #"timestamp"}];
messageMap.forceCollectionMapping = YES;
RKResponseDescriptor *descriptor =
[RKResponseDescriptor
responseDescriptorWithMapping:messageMap
method:RKRequestMethodGET
pathPattern:#"/v1/app/e51cb2dd24af47a49232b942210e758d/text?f=test"
keyPath:#"result"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objmgr addResponseDescriptor:descriptor];
}
- (IBAction)helloButtonClicked:(id)sender {
[[RKObjectManager sharedManager].HTTPClient setDefaultHeader:#"X-App-ApiKey" value:#"2c130c75dc9f4c2c8ef7c8753e8b7c56"];
NSLog(#"ResponseDescriptors %#", [[RKObjectManager sharedManager] responseDescriptors]);
[[RKObjectManager sharedManager] getObjectsAtPath:#"/v1/app/e51cb2dd24af47a49232b942210e758d/text?f=test" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
_Response.text = [mappingResult.dictionary objectForKey:#"messagebody"];
} failure:^(RKObjectRequestOperation *operation, NSError *error)
{
_Response.text = #"Something went wrong.";
}];
}
Here's the error from the trace:
2014-08-21 00:12:30.653 The-App[13924:3707] E restkit.network:RKObjectRequestOperation.m:208 GET 'http:///v1/app/e51cb2dd24af47a49232b942210e758d/text?f=test' (200 OK / 0 objects) [request=2.7297s mapping=0.0000s total=2.7365s]:
error=Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No response descriptors match the response loaded." UserInfo=0xb164380 {NSErrorFailingURLStringKey=http:///v1/app/e51cb2dd24af47a49232b942210e758d/text?f=test, NSLocalizedFailureReason=A 200 response was loaded from the URL 'http:///v1/app/e51cb2dd24af47a49232b942210e758d/text?f=test', which failed to match all (0) response descriptors:, NSLocalizedDescription=No response descriptors match the response loaded., keyPath=null, NSErrorFailingURLKey=http:///v1/app/e51cb2dd24af47a49232b942210e758d/text?f=test, NSUnderlyingError=0xb1641c0 "No mappable object representations were found at the key paths searched."}
response.body={"success":{},"errors":{},"result":{"messagebody":"Hello!","timestamp":"2014-08-21T04:12:28.4689099+00:00"}}
Any ideas here on how to troubleshoot? I've been banging my head on this one for a few hours and google has not been my friend.
EDIT: The Response Descriptors
2014-08-22 10:41:21.580 Apprenda-CloudMine-App[10987:60b] ResponseDescriptors (
"<RKResponseDescriptor: 0x17826c180 method=(GET) pathPattern=/v1/app/e51cb2dd24af47a49232b942210e758d/text?f=test keyPath=result statusCodes=200-299 : <RKObjectMapping:0x17826bfc0 objectClass=Message propertyMappings=(\n \"<RKAttributeMapping: 0x1780499f0 timestamp => timestamp>\",\n \"<RKAttributeMapping: 0x17804cf30 messagebody => messagebody>\"\n)>>"
)
Thanks!
-Chris
That error indicates that the path between your request and your response descriptor are not matching. It is subtle, the (0) response descriptors in the error message also indicate to me that there are no response descriptors for that path. For more information on the error messages for RestKit path mismatches see this GitHub issue.
Why then are you getting a path mismatch? After all, the path in your request is exactly the same as your path in your response descriptor. It would seem that your problem is likely the GET parameter specified in the response descriptors path. You need to remove the get parameter from your response descriptors path since it is not technically part of the path.
It should be the following instead.
[RKResponseDescriptor
responseDescriptorWithMapping:messageMap
method:RKRequestMethodGET
pathPattern:#"/v1/app/e51cb2dd24af47a49232b942210e758d/text"
keyPath:#"result"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
Thanks to Flaviu Simihaian for realizing this.

Restkit 0.22.0 and iOS 7 RKObjectManager postObject not generating JSON data

I receive this error when I attempt to use postObject in XCODE 5 for iOS 7:
Here is the error message without the URLs which point to https:// service
E restkit.network:RKObjectRequestOperation.m:542 Object request failed: Underlying HTTP request operation failed with error: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1016 "Expected content type {(
"application/x-www-form-urlencoded",
"application/json"
)}, got text/html" UserInfo=0xb9a44f0 {NSLocalizedRecoverySuggestion={"status": "ok"}, AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest: 0x8dd14c0> { URL: http:
NSLocalizedDescription=Expected content type {(
"application/x-www-form-urlencoded",
"application/json"
)}, got text/html, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xb998d30>
{ status code: 200, headers {
Connection = close;
"Content-Length" = 16;
"Content-Type" = "text/html; charset=utf-8";
Any object the I put in postObject method just returns text/html and not JSON. I does not appear to use the requestDescriptor or requestMapping that I defined. Its simply posting the object as text/html.
Is there another method of posting using RestKit?
Is there a fix or workaround for this issue?
Here's my code sample:
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping]; // objectClass == NSMutableDictionary
[requestMapping addAttributeMappingsFromArray:#[#"email", #"nickname"]];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[RegisterDeviceInfo class] rootKeyPath:nil method:RKRequestMethodAny];
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:_uuidURL]];
[RKObjectManager setSharedManager:objectManager];
[objectManager addRequestDescriptor:requestDescriptor];
RegisterDeviceInfo *deviceInfo = [RegisterDeviceInfo new];
deviceInfo.email = email;
deviceInfo.nickname = nickname;
// This is not accepted by the postObject and mapped to JSON object using above mappings and requestDescriptor
NSLog(#"deviceInfo is: %#", deviceInfo);
[[RKObjectManager sharedManager] postObject:deviceInfo path:(_uuidURL) parameters:nil success: nil failure: nil];
NSLog(#"deviceInfo is: %#", deviceInfo);
You should show the rest of the error (the lines before what you show). This is about the response, not the request. An HTML response often means an error message and you need to see what that is to know what's wrong.
The issue appears to be that you are using (_uuidURL) as the path when you call postObject but that it is the full URL.
The path should be relative to the base URL that was used to create the object manager. i.e.
baseURL = #"www.myserver.com/"
path = #"mycontent"

Restkit json error response msg from server

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.
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.
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
}
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!
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.

Specify a specific mapping for objects in JSON array in RESTkit?

I'm receiving this JSON from django-rest-framework when I request a list of UserProfile objects:
[
{
"gender": 1,
"show_gender": true,
"show_real_name": true
},
{
"gender": 2,
"show_gender": true,
"show_real_name": true
}
]
But I'm at loss how to configure my mappings with Restkit. Restkit seems to expect a dictionary instead of a list, since it seems to use the dictionary's keys as "KeyPath" to identify the necessary mappings. Is there a way to specify a mapping for the received objects manually?
Got it!
// define a mapping
RKObjectMapping* userMapping = [RKObjectMapping mappingForClass:[ERUser class]];
[userMapping mapAttributes:
#"gender",
#"show_gender",
#"show_real_name",
nil];
// add the mapping anonymously
[objectManager.mappingProvider addObjectMapping:userMapping];
// tell the mapping provider which mapping to use
RKObjectMapping* articleMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[ERUser class]];
[objectManager loadObjectsAtResourcePath:#"/users" objectMapping:articleMapping delegate:self];

Resources