How to pretty-print an NSError object in Xcode console?
Solution 1
userInfo is a NSDictionary
NSLog(@" error => %@ ", [errorOrNil userInfo] )
Prints something like this for me
error => {
NSLocalizedDescription = "User already exists";
NSLocalizedFailureReason = "";
NSLocalizedRecoverySuggestion = "Retry request based on information in `NSLocalizedFailureReasonErrorKey`";
kinveyErrorCode = UserAlreadyExists;
kinveyInternalErrorString = "";
kinveyRequestId = e5be0aed155e4925b3365d57de3dc5b2;
}
You can also try:
NSLog(@" error => %@ ", [errorOrNil localizedDescription] )
Which prints out:
You got an error: User already exists
Solution 2
Not a very cool solution - you can write your own category for the NSError class and represent the text as you want.
Chaitanya Gupta
Updated on July 20, 2022Comments
-
Chaitanya Gupta almost 2 years
I printed an NSError object in the Xcode console (via
NSLog(@"%@", error);
) and for a certain kind of error, this is what I get:Domain=NSCocoaErrorDomain Code=133020 "The operation couldn’t be completed. (Cocoa error 133020.)" UserInfo=0xe939170 {conflictList=( "NSMergeConflict (0xe93cad0) for NSManagedObject (0x5dba970) with objectID '0x5dc26f0 <x-coredata://775D53AE-58A4-4B18-BA52-D46781A183AE/SomeObject/p1>' with oldVersion = 2 and newVersion = 3 and old object snapshot = {\n creationDate = \"2011-08-24 06:52:22 +0000\";\n prop1 = \"a65e349a-b315-488e-b7f8-e459e353fd6e\";\n username = \"test-user\";\n password = \"foobar\";\n} and new cached row = {\n creationDate = \"2011-08-24 06:52:22 +0000\";\n prop1 = \"a65e349a-b315-488e-b7f8-e459e353fd6e\";\n username = \"test-user\";\n password = \"foobar\";\n}"
When I replace all the '\n's with newline and all the \"s with " in emacs, I get a much nicely formatted error message:
Domain=NSCocoaErrorDomain Code=133020 "The operation couldn’t be completed. (Cocoa error 133020.)" UserInfo=0xe939170 {conflictList=( "NSMergeConflict (0xe93cad0) for NSManagedObject (0x5dba970) with objectID '0x5dc26f0 <x-coredata://775D53AE-58A4-4B18-BA52-D46781A183AE/SomeObject/p1>' with oldVersion = 2 and newVersion = 3 and old object snapshot = { creationDate = "2011-08-24 06:52:22 +0000"; prop1 = "a65e349a-b315-488e-b7f8-e459e353fd6e"; username = "test-user"; password = "foobar"; } and new cached row = { creationDate = "2011-08-24 06:52:22 +0000"; prop1 = "a65e349a-b315-488e-b7f8-e459e353fd6e"; username = "test-user"; password = "foobar"; }"
I would much prefer to see this nicely formatted error message in Xcode itself rather than copy-paste it and search-and-replace characters in another editor. Is there a way to do this?
EDIT For clarity, the error is generated by a core data save operation:
NSError *error if (![context save:&error]) { NSLog(@"%@", error); }
The offending part of the error object in this case (from where the \n's and \"s are being printed) is the value of the
conflictList
key in the error'suserInfo
dictionary. -
Chaitanya Gupta over 12 yearsInteresting solution -- but I am looking for a more general once since I don't know what kind of errors I will get in the future. e.g. In this case, the @"conflictList" key of the error's
userInfo
dictionary is the culprit. This will not always be the case.