Skip to content

Commit

Permalink
Catch key path exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
robb committed Mar 9, 2014
1 parent d944064 commit 0644728
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions Mantle/MTLJSONAdapter.m
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,21 @@ - (id)initWithJSONDictionary:(NSDictionary *)JSONDictionary modelClass:(Class)mo
NSString *JSONKeyPath = [self JSONKeyPathForPropertyKey:propertyKey];
if (JSONKeyPath == nil) continue;

id value = JSONDictionary;
NSArray *JSONKeyPathComponents = [JSONKeyPath componentsSeparatedByString:@"."];
for (NSString *itemJSONKeyPathComponent in JSONKeyPathComponents) {
if (![value isKindOfClass:NSDictionary.class]) {
if (error != NULL) {
NSDictionary *userInfo = @{
NSLocalizedDescriptionKey: NSLocalizedString(@"Invalid JSON dictionary", @""),
NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:NSLocalizedString(@"%@ could not be parsed because an invalid JSON dictionary was provided for key path \"%@\"", @""), modelClass, JSONKeyPath],
};

*error = [NSError errorWithDomain:MTLJSONAdapterErrorDomain code:MTLJSONAdapterErrorInvalidJSONDictionary userInfo:userInfo];
}

return nil;
id value;
@try {
value = [JSONDictionary valueForKeyPath:JSONKeyPath];
} @catch (NSException *ex) {
if (error != NULL) {
NSDictionary *userInfo = @{
NSLocalizedDescriptionKey: NSLocalizedString(@"Invalid JSON dictionary", nil),
NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:NSLocalizedString(@"%1$@ could not be parsed because an invalid JSON dictionary was provided for key path \"%2$@\"", nil), modelClass, JSONKeyPath],
MTLJSONAdapterThrownExceptionErrorKey: ex
};

*error = [NSError errorWithDomain:MTLJSONAdapterErrorDomain code:MTLJSONAdapterErrorInvalidJSONDictionary userInfo:userInfo];
}

value = [value valueForKey:itemJSONKeyPathComponent];
if (value == nil || value == NSNull.null) break;
return nil;
}

if (value == nil) continue;
Expand Down

0 comments on commit 0644728

Please sign in to comment.