hapijs / bounce

Selective error catching and rewrite rules
Other
176 stars 13 forks source link

Support AbortSignal #37

Open kanongil opened 11 months ago

kanongil commented 11 months ago

This is done in 2 ways:

  1. Add a signal option to rethrow() and ignore().
  2. Add extra functions to identify AbortSignal derived aborts and timeouts.

Regarding 1, this allows a Bounce user to simply hook a signal.rethrowIfAborted() into the call. Eg.:

function doStuff(options) {

    const signal = options.signal;
    try {
        const res = await fetch(options.url, { signal });
    }
    catch (err) {
        Bounce.rethrow(err, 'system', { signal });

        // handle non-system, non-signalled error, maybe with a retry
    }
}

Alternatively, you would need an extra signal.rethrowIfAborted() before the call to Bounce, which gets verbose:

signal.rethrowIfAborted();
Bounce.rethrow(err, 'system');

Regarding 2, the default errors thrown when calling abort() or timeout() have no shared prototype and have changed in various node releases. As such you cannot do an instanceof check with eg. Bounce.rethrow(err, AbortError). The most compatible way to detect this, is to simply check that the name property is AbortError. Given this, I have added shortcuts to do it safely.

Note that I have marked these as breaking, not because of the API changes, but solely because it requires more modern versions of node (v16.17+) then the current release.

devinivy commented 9 months ago

I'd say yes—I hope and expect we are able to incorporate abort signals into hapi itself as well!

Marsup commented 1 week ago

I'll fix the CI.

Marsup commented 1 week ago

You can now rebase both your PRs, they should pass.