Method decorator that translates errors thrown by a method according to an
ordered list of rules. Unmatched errors are rethrown as-is, so domain
exceptions and genuine bugs pass through untouched. Order rules by
specificity: a subclass error rule must precede its superclass rule. Have each
rule's to pass the original error as cause to preserve its stack chain.
Apply it to a single method, or to a whole class to wrap every instance
method. The class form takes an optional leading options object to narrow the
set with include/exclude. By default the rules run as a pipeline — each
matching rule's output feeds the next (A → B → C); pass pipeline: false to
stop at the first match.
@MapErrors(rule1, rule2) // every instance method
@MapErrors({ exclude: ["healthCheck"] }, rule1) // all but healthCheck
@MapErrors({ pipeline: false }, ruleA, ruleB) // stop at first match
class Service {}
When a method is reached by more than one annotation — its own method-level
@MapErrors, its class's, and any annotated ancestor's — the rule lists are
merged most-specific-first (method > child-class > parent-class); the
most-specific annotation also decides the pipeline mode. The effective list
is resolved from the runtime receiver, so a subclass's class-level rules apply
to methods it inherits.
The wrapper preserves each method's return type, name, and arity (length)
— synchronous methods stay synchronous, async methods stay async; rejections
are mapped on the promise.
Works under both legacy experimentalDecorators and TC39 Stage-3 decorators;
the returned function detects the standard and the target at runtime.
Method decorator that translates errors thrown by a method according to an
ordered list of rules. Unmatched errors are rethrown as-is, so domain
exceptions and genuine bugs pass through untouched. Order rules by
specificity: a subclass error rule must precede its superclass rule. Have each
rule's to pass the original error as cause to preserve its stack chain.
Apply it to a single method, or to a whole class to wrap every instance
method. The class form takes an optional leading options object to narrow the
set with include/exclude. By default the rules run as a pipeline — each
matching rule's output feeds the next (A → B → C); pass pipeline: false to
stop at the first match.
@MapErrors(rule1, rule2) // every instance method
@MapErrors({ exclude: ["healthCheck"] }, rule1) // all but healthCheck
@MapErrors({ pipeline: false }, ruleA, ruleB) // stop at first match
class Service {}
When a method is reached by more than one annotation — its own method-level
@MapErrors, its class's, and any annotated ancestor's — the rule lists are
merged most-specific-first (method > child-class > parent-class); the
most-specific annotation also decides the pipeline mode. The effective list
is resolved from the runtime receiver, so a subclass's class-level rules apply
to methods it inherits.
The wrapper preserves each method's return type, name, and arity (length)
— synchronous methods stay synchronous, async methods stay async; rejections
are mapped on the promise.
Works under both legacy experimentalDecorators and TC39 Stage-3 decorators;
the returned function detects the standard and the target at runtime.
Method decorator that translates errors thrown by a method according to an ordered list of rules. Unmatched errors are rethrown as-is, so domain exceptions and genuine bugs pass through untouched. Order rules by specificity: a subclass error rule must precede its superclass rule. Have each rule's
topass the original error ascauseto preserve its stack chain.Apply it to a single method, or to a whole class to wrap every instance method. The class form takes an optional leading options object to narrow the set with
include/exclude. By default the rules run as a pipeline — each matching rule's output feeds the next (A → B → C); passpipeline: falseto stop at the first match.When a method is reached by more than one annotation — its own method-level
@MapErrors, its class's, and any annotated ancestor's — the rule lists are merged most-specific-first (method > child-class > parent-class); the most-specific annotation also decides thepipelinemode. The effective list is resolved from the runtime receiver, so a subclass's class-level rules apply to methods it inherits.The wrapper preserves each method's return type,
name, and arity (length) — synchronous methods stay synchronous, async methods stay async; rejections are mapped on the promise.Works under both legacy
experimentalDecoratorsand TC39 Stage-3 decorators; the returned function detects the standard and the target at runtime.