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.
Creating child domains
Section titled “Creating child domains”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: { /* ... */ },});Ancestry checks
Section titled “Ancestry checks”err_app.isParentOf(err_auth_registration); // trueerr_auth.isParentOf(err_auth_registration); // trueerr_auth_registration.isParentOf(err_auth); // falseType guards
Section titled “Type guards”// Exact domain match onlyerr_auth.isExact(someError); // true only for the err_auth domain
// This domain OR any descendanterr_auth.isThisOrChild(someError); // true for err_auth and all its childrenUse 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.
When to nest
Section titled “When to nest”- Group errors by bounded context:
err_app→err_auth→err_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.