hapijs / hoek

Node utilities shared among the extended hapi universe
Other
481 stars 171 forks source link

Refactor AssertError #382

Closed kanongil closed 1 year ago

kanongil commented 2 years ago

This is a more comprehensive version of #381 that I made last autumn as part of my investigations into a node 14 upgrade.

I did a few things…

  1. Move AssertError to errors.js and make it a named export.
  2. Set the name property to AssertError.
  3. Refactor to make the message massaging a part of Hoek.assert() instead of the error itself. This means that a lib that imports to test for the AssertError type, won't also have to load the stringify.js module.
  4. Allow creator to specify a custom constructor function to simplify the stack trace.
  5. Fix error in assert() simplification of the stack trace (exports.assert was always undefined).
  6. Additional testing.

I also took the liberty to add the throws condition clause to the assert() typings, which can help narrow type signatures.

Note that this is a breaking change due to the renaming, but also from the updated constructor signature.