Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error Code Reference for OSX/Cocoa

Tags:

xcode

macos

cocoa

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 image 591
Kristopher Johnson Avatar asked Sep 01 '08 20:09

Kristopher Johnson


People also ask

What is NSError code?

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 .

What is NSError domain?

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.

What is NSError object made of?

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.

What is an NSError in Swift?

Information about an error condition including a domain, a domain-specific error code, and application-specific information.


2 Answers

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.

like image 185
Chris Hanson Avatar answered Sep 22 '22 13:09

Chris Hanson


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
like image 25
Mark Amery Avatar answered Sep 21 '22 13:09

Mark Amery