Log userInfo, not localizedDescription, for CoreData errors

Posted by on Oct 12, 2010 in code | No Comments

Most of the Core Data code samples out there log [error localizedDescription] on errors. However, that will return a message that isn’t particularly useful, like:

The operation couldn’t be completed. (Cocoa error 1570.)

Instead, if you log [error userInfo], you’ll get a much more actionable message, like:

 {
    NSLocalizedDescription = "The operation couldnU2019t be completed. (Cocoa error 1570.)";
    NSValidationErrorKey = timestamp;
    NSValidationErrorObject = "<QueuedOperation: 0x920b100> (entity: QueuedOperation; id: 0x920c700 <x-coredata:///QueuedOperation/t60E29330-FD70-4E70-B5AB-CC7C234FF70965> ; data: {n    identifier = 432;n    timestamp = nil;n    type = 1;n})";
}

From that error, I can immediately tell that the object didn’t pass validation because the timestamp field was nil. When your users start reporting some obscure error, this kind of detail can be critical to troubleshooting.

Leave a Reply