Skip to content

NSError: domain property ​

The domain property is the error domain: which part of the system the error came from.

Value ​

A string.

  • Read-only. Assigning to it throws TypeError, and the value doesn't change.
  • It's the system's original value.

The error domains each API gives:

domainFrom
'NSCocoaErrorDomain'File access (application.fileSystem)
'NSOSStatusErrorDomain'The keychain (application.keychain)
'kCLErrorDomain'Location (CLLocationManager)
'com.apple.LocalAuthentication'Face ID (LAContext)
'UNErrorDomain'Notifications (UNUserNotificationCenter)
'NSURLErrorDomain'Web page loading (webContents)
An empty stringnew AVCaptureDeviceInput() failing to open the camera

The table shows the usual cases; the system can also give a domain not listed here. When checking an error code, compare domain first.

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) {
      console.log(cause.domain);
    }
  }
}
main();

Result: Prints NSCocoaErrorDomain (measured).

Corresponding iOS API ​

NSError.domain.

See also ​

  • code: the number within the error domain