Skip to content

Domain Hierarchies

Compose domains into parent/child trees and match across ancestry.

Domains can nest. A child domain inherits its place in the tree, so a handler can match a broad parent without enumerating every child’s ids.

const err_app = defineNiceError({ domain: "err_app", schema: {} } as const);
const err_auth = err_app.createChildDomain({
domain: "err_auth",
schema: { /* ... */ },
});
const err_auth_registration = err_auth.createChildDomain({
domain: "err_auth_registration",
schema: { /* ... */ },
});
err_app.isParentOf(err_auth_registration); // true
err_auth.isParentOf(err_auth_registration); // true
err_auth_registration.isParentOf(err_auth); // false
// Exact domain match only
err_auth.isExact(someError); // true only for the err_auth domain
// This domain OR any descendant
err_auth.isThisOrChild(someError); // true for err_auth and all its children

Use isExact when you want to handle exactly one domain’s errors; use isThisOrChild to catch a whole subtree — e.g. “any auth-related error” regardless of which specific child domain produced it.

  • Group errors by bounded context: err_apperr_autherr_auth_registration.
  • Let middleware match broadly (err_auth.isThisOrChild) while leaf code matches precisely (err_auth_registration.isExact).
  • Keep domain strings stable — they’re part of the serialized identity.

Next: Serialization & transport →