[whatwg/dom] Expose `signal.reason` as `error.cause` for `AbortError` (Issue #1090)

Current situation with `AbortSignal` aborting looks confusing.
```mjs
fetch('foo://bar', {
  signal: AbortSignal.timeout(100)
}).catch(errorHandler);

async function errorHandler(err) {
  /* what `err` should have there to be useful? */
};
```
As an user, I want to handle `err` and know what happened. There are three main possibilities:
- `AbortError`: in case of more complicated code, a manual/conditional cancellation
- `TimeoutError`: standardized timeout
- everything else: generic errors, specific for `fetch` in this case

In current implementations (Chrome 103.x, Firefox 101.x, node 18.x), `err` is an `AbortError`. There's no way to know it's a timeout, nor to retrieve `signal.reason` if any.
From my understanding of current specs and plans on applying them to `fetch` in particular, `err` should be the `signal.reason` itself: `TimeoutError` in this example or arbitrary non-undefined value in general.

"Current" version has an obvious issue: we can't distinguish timeout from manual abortion, and can't distinguish different abortion reasons.
"Future" version has another issue: it breaks backwards compatibility with userland code that relied on `AbortError`, and more importantly, disallows us to distinguish abortion errors from everything else.

With Error Cause proposal being included in ES2022, it became possible to chain errors. It sounds perfectly reasonable to me if functions aborted by `AbortSignal` will always throw an abortion caused by signal, i.e. `AbortError` having `signal.reason` in its `cause` property.

- It would keep backwards compatibility with userland code that relies on `err?.name === 'AbortError'` robustness
- It would ensure that any new standards, similar to `TimeoutError`, will be still recognized as aborts
- It would allow handling various abortion reasons separately from arbitrary errors

Is there anything that makes this concept impossible or unwanted?

-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/1090
You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/dom/issues/1090@github.com>

Received on Monday, 27 June 2022 10:06:45 UTC