Skip to content

NSError: code property ​

The code property is the error's number within its error domain.

Value ​

A number.

  • Read-only. Assigning to it throws TypeError, and the value doesn't change.
  • It's the system's original value.
  • The same number means different things in different error domains; check it together with domain.

Some common values:

domaincodeMeaning
'NSCocoaErrorDomain'260The file to read doesn't exist (measured)
'NSURLErrorDomain'-999Loading was canceled, for example by calling stop() (measured)
'NSURLErrorDomain'-1100The local file to load doesn't exist (measured)
'kCLErrorDomain'1No location authorization
'com.apple.LocalAuthentication'-8Biometrics locked after too many failed attempts

For what each number means in an error domain, look up that domain's error code list in Apple's documentation.

Examples ​

js
/// <reference types="@nacrejs/types" />

async function main() {
  const fs = application.fileSystem;
  try {
    await fs.readText(fs.documents + '/missing.txt');
  } catch (err) {
    const cause = err instanceof Error ? err.cause : undefined;
    if (cause instanceof NSError && cause.domain === 'NSCocoaErrorDomain' && cause.code === 260) {
      console.log('File not found');
    }
  }
}
main();

Result: Prints File not found (the error code was measured).

Corresponding iOS API ​

NSError.code.

See also ​