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)?
Like exit status codes, an NSError -code signals the nature of the problem. These status codes are defined within a particular error domain , in order to avoid overlap and confusion. These status codes are generally defined by constants in an enum .
The core attributes of an NSError object—or, simply, an error object—are an error domain, a domain-specific error code, and a “user info” dictionary containing objects related to the error, most significantly description and recovery strings.
The core attributes of an NSError object are an error domain (represented by a string), a domain-specific error code and a user info dictionary containing application specific information.
Information about an error condition including a domain, a domain-specific error code, and application-specific information.
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.
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:
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)
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With