Skip to content

handler side effect

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

SIDE_EFFECT

Synopsis

Perform some action and continue with the handler chain using the current error or immediately throw the error thrown by the callback.

Description

This handler allows execution of a callback, anticipating that it might fail. If it does, the error from the callback is thrown immediately. If the callback executes without errors, the current error is propagated to the next handler in the chain.

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).
  • onCallbackError: CallbackErrorAction (optional, defaults to THROW_WRAPPED)
    This option controls how errors thrown by the callback are handled.
    • THROW_WRAPPED: The error is wrapped in an MapError. This is the default.
    • THROW: The error is thrown as-is.
    • THROW_TRIGGER: The error is ignored and the triggering error is thrown instead.

Example

class Foo {
    @HandleError({
        action: HandlerAction.SIDE_EFFECT,
        // Let's assume the database library knows how to serialize errors ;)
        callback: error => this.database.insert('errors', error),
    })
    public async doSomething(): Promise<void> {
        // log all errors thrown here to the database (and fail if the insertion fails)
    }
}