[whatwg/streams] rethrowAssertionErrorRejection is broken (#865)

84c584c replaced the default Node `assert` with the `better-assert` module, in order to resolve #807. However, this refactoring accidentally broke the `rethrowAssertionErrorRejection` helper, since [it tries](https://github.com/whatwg/streams/blob/a480a4fd23c1e3f0cea94cbbdf224f352ae60ab2/reference-implementation/lib/utils.js#L8) to use `assert.AssertionError`.

While `assert` does indeed export an `AssertionError` class, `better-assert` does not. Therefore, `assert.AssertionError` is `undefined`, and `e.constructor === assert.AssertionError` always evaluates to `false`. As such, the helper never re-throws an assertion error.

One possibility would be to use `require('assert').AssertionError`, but this assumes `better-assert` will never introduce a different error class. If necessary, we could detect the error class with something like:
```js
const AssertionError = (() => {
  try {
    assert(false);
  } catch (e) {
    return e.constructor;
  }
})();
```
But maybe let's not go *that* far. 😛 

Thoughts? I can make a quick PR to use `require('assert').AssertionError` again.

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

Received on Tuesday, 26 December 2017 18:35:12 UTC