Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pretty-print an NSError object in Xcode console?

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's userInfo dictionary.

like image 752
Chaitanya Gupta Avatar asked Aug 24 '11 07:08

Chaitanya Gupta


2 Answers

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 
like image 190
superlogical Avatar answered Sep 22 '22 12:09

superlogical


Not a very cool solution - you can write your own category for the NSError class and represent the text as you want.

like image 44
d.lebedev Avatar answered Sep 25 '22 12:09

d.lebedev