Question

If I get an error code result from a Cocoa function, is there any easy way to figure out what it means (other than by grepping through all the .h files in the framework bundles)?

Was it helpful?

Solution

You should look at the <Framework/FrameworkErrors.h> header for whatever framework the method you're using that's returning an error comes from.

For example, an NSError in the Cocoa domain that you get from a method in the Foundation framework will have its code property described in the <Foundation/FoundationErrors.h> header. Similarly with AppKit and <AppKit/AppKitErrors.h> and Core Data and <CoreData/CoreDataErrors.h>.

Also, if you print the description of the NSError in the debugger, it should include not only the error domain and code, but also the name of the actual error code constant so you can look it up in the API reference.

OTHER TIPS

The sections on 'Error Domains' and 'Error Codes' in Apple's Error Handling Programming Guide address this reasonably well. You need to do the following:

  1. Log the error, taking note of both the error domain (a human-readable / Googleable string that tells you where to look for the error code definitions) and the error code itself (an integer)

  2. Sniff around on Google (or read from the list below) and figure out the name of the header file(s) where the error codes for that error domain are defined

  3. Search those header file(s) for the error code you got. You should find both a constant name for the error code (like ENOMEM), and hopefully also an explanatory comment (like /* Cannot allocate memory */) explaining what the error means. If there's no comment, and the constant name isn't self-explanatory, just Google the constant name and you'll probably find a proper description.

Some header files of major error domains:

NSCocoaErrorDomain

Error code declarations are spread across three header files:

  • <Foundation/FoundationErrors.h> (Generic Foundation error codes)
  • <AppKit/AppKitErrors.h> (Generic AppKit error codes)
  • <CoreData/CoreDataErrors.h> (Core Data error codes)

NSURLErrorDomain

Check NSURLError.h

NSXMLParserErrorDomain

CheckNSXMLParser.h

NSMachErrorDomain

Check /usr/include/mach/kern_return.h

NSPOSIXErrorDomain

Check /usr/include/sys/errno.h

NSOSStatusErrorDomain

Check

/System/Library/Frameworks/CoreServices.framework/Frameworks/CarbonCore.framework/Headers/MacErrors.h

Also, Cocoa's NSError is meant to be displayable to the end user. If you just log it, it should be readable.

If you're talking about Carbon's OSStatus and such, MacErrors.h.

For NSError errors add a line of code:

NSError     *error;

// ... Some code that returns an error

// Get the error as a string
NSString *s = [error localizedDescription];

// Observe the code for yourself or display to the user.   
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top