Domain Hierarchies
Nest domains into parent/child trees and match a whole subtree at once.
Domains can nest. A child domain knows its place in the tree, so a handler can match a broad parent without listing every child’s ids.
Creating child domains
Section titled “Creating child domains”const err_app = defineNiceError({ domain: "err_app", schema: {} });
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 to handle exactly one domain’s errors. Use isThisOrChild to catch a whole subtree, such as “any auth-related error”, whichever 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.