RestKit/RestKit — Issue #431: SIGABRT when the server returns an error with json key matching one of the key paths
Author: rantav
Created: 2011-11-03 12:30:09+00:00
State: open

This issue was previously reported here but at the time I knew less of the reason for the error. Now I'm able to report that as a bug.
https://groups.google.com/forum/#!searchin/restkit/SIGABRT/restkit/h6oH9Y4rcyE/LGVO2fK4XK0J

I have an object mapping with the following line:

[manager.mappingProvider setMapping:mapping forKeyPath:@"message"];

Coincidentally, when my server returns an error it would do:

def error!(message, status)
render :json => {:message => message}, :status => status
end

which results, for example in case of 401:

401 ...
{message: "401 access denied"}

When this happens restkit realizes it's an error b/c of the 401 response code but even in this situation restkit would still try to map the response to an object. When doing so it would crash my app with SIGABRT and with the following stack trace:

````
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key id.'
*** Call stack at first throw:
(
0 CoreFoundation 0x33af464f __exceptionPreprocess + 114
1 libobjc.A.dylib 0x35fe7c5d objc_exception_throw + 24
2 CoreFoundation 0x33af43cd -[NSException dealloc] + 0
3 Foundation 0x33bc1fdb -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 186
4 Foundation 0x33b6609d _NSGetUsingKeyValueGetter + 88
5 Foundation 0x33b65fbf -[NSObject(NSKeyValueCoding) valueForKey:] + 190
6 Foundation 0x33b5c2ff -[NSObject(NSKeyValueCoding) valueForKeyPath:] + 126
7 invi 0x000906e3 -[RKManagedObjectMapping mappableObjectForData:] + 686
8 invi 0x000c6071 -[RKObjectMapper objectWithMapping:andData:] + 696
9 invi 0x000c5107 -[RKObjectMapper mapObject:atKeyPath:usingMapping:] + 810
10 invi 0x000c6941 -[RKObjectMapper performMapping] + 2172
11 invi 0x000b90f9 -[RKObjectLoader mapResponseWithMappingProvider:toObject:error:] + 1056
12 invi 0x000b9def -[RKObjectLoader handleResponseError] + 82
13 invi 0x0008ecff -[RKManagedObjectLoader handleResponseError] + 46
14 invi 0x000b9d5d -[RKObjectLoader isResponseMappable] + 928
15 invi 0x000ba891 -[RKObjectLoader didFinishLoad:] + 740
16 invi 0x000adf79 -[RKResponse connectionDidFinishLoading:] + 40
17 Foundation 0x33b6c2f5 -[NSURLConnection(NSURLConnectionReallyInternal) sendDidFinishLoading] + 68
18 Foundation 0x33b6c277 _NSURLConnectionDidFinishLoading + 78
19 CFNetwork 0x30987411 _ZN19URLConnectionClient23_clientDidFinishLoadingEPNS_26ClientConnectionEventQueueE + 136
20 CFNetwork 0x3097bf45 _ZN19URLConnectionClient26ClientConnectionEventQueue33processAllEventsAndConsumePayloadEP20XConnectionEventInfoI12XClientEvent18XClientEventParamsEl + 100
21 CFNetwork 0x3097be37 _ZN19URLConnectionClient13processEventsEv + 70
22 CFNetwork 0x3097bde9 _ZN13URLConnection24multiplexerClientPerformEP18RunLoopMultiplexer + 36
23 CFNetwork 0x3097bd5b _ZN17MultiplexerSource7performEv + 126
24 CFNetwork 0x3097bcd9 _ZN17MultiplexerSource8_performEPv + 8
25 CoreFoundation 0x33acba79 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 12
26 CoreFoundation 0x33acd75f __CFRunLoopDoSources0 + 382
27 CoreFoundation 0x33ace4eb __CFRunLoopRun + 230
28 CoreFoundation 0x33a5eec3 CFRunLoopRunSpecific + 230
29 CoreFoundation 0x33a5edcb CFRunLoopRunInMode + 58
30 GraphicsServices 0x331c441f GSEventRunModal + 114
31 GraphicsServices 0x331c44cb GSEventRun + 62
32 UIKit 0x34096d69 -[UIApplication _run] + 404
33 UIKit 0x34094807 UIApplicationMain + 670
34 invi 0x000029b7 main + 122
35 invi 0x00002400 start + 40
)

terminate called after throwing an instance of 'NSException'
````

Restkit thinks this is a valid Message object b/c it responds to the same keyPath "message"
I'll fix my server to return instead something like this to prevent this name clash:

{error_message: "401 access denied"}

So this fixes the problem for me. It'd be a good idea to fix restkit as well.
thanks