Skip to content

handler tap

Raphael Pigulla edited this page Apr 22, 2022 · 1 revision

TAP

Synopsis

Perform some action, ignoring exception thrown by it, and continue with the handler chain using the current error.

Description

This handler allows execution of a callback without affecting the current error (which is simply propagated to the next handler in the chain). Any exception thrown by the callback itself is ignored.

A TAP handler is the only handler that is guaranteed not to alter the behaviour of the wrapped function (unless it explicitly terminates the application, causes an infinite loop, exhausts available memory or causes some other catastrophic error). It is basically a shorthand for SIDE_EFFECT with callbackErrorOption set to THROW_TRIGGER.

Options

The following options are supported.

  • callback: (error, ...parameters) => void | Promise<void> (mandatory)
    The callback is invoked with the same context as the wrapped function (unless you use an arrow function).
  • scope: Class<Error> (optional)
    Limit the handler to exceptions of the given class (or any subclass thereof).
  • predicate: (error, ...parameters) => boolean | Promise<boolean> (optional)
    Dynamically skip invocation of the handler depending on the error thrown or any of the wrapped method's parameters. The predicate is invoked with the same context as the wrapped function (unless you use an arrow function).

Example

class Foo {
    @HandleError({
        action: HandlerAction.TAP,
        callback: error => console.error(error),
    })
    public async doSomething(): Promise<void> {
        // no error will go unlogged :)
    }
}