boom icon indicating copy to clipboard operation
boom copied to clipboard

Add support for native `cause`

Open matthieusieben opened this issue 1 year ago • 6 comments

Support plan

  • is this issue currently blocking your project? (yes/no): no
  • is this issue affecting a production system? (yes/no): no

Context

  • node version: n.a.
  • module version: n.a.
  • environment (e.g. node, browser, native): n.a.
  • used with (e.g. hapi application, another framework, standalone, ...): n.a.
  • any other relevant information: n.a.

What problem are you trying to solve?

Errors now have a cause property described here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause

We should be able to easily set the cause when creating a Boom error.

Currently, the following must be done:

try {
  // Stuff
} catch (err) {
  throw Object.assign(badImplementation('Stuff should not throw an error'), { cause: err })
}

Do you have a new or modified API suggestion to solve the problem?

It is my understanding that currently, in the Hapi ecosystem, the data property of Boom errors is sometimes used to contain the original error, so anything proposed here will likely largely impact the Hapi ecosystem.

in order not to break the current api too much, we could just add a new option on error creators:

export function badImplementation<Data>(message?: string, data?: Data, errorOptions?: { cause?: unknown }): Boom<Data>;

and specify that from now on, data should no longer be used to contain the originating error.

function sqlQuery(query, values) {
  try {
    // Execute some SQL
  } catch (err) {
    throw internal('Failed to query DB', { code: "sql-query-error", query, values }, { cause: err })
  }
}

matthieusieben avatar Mar 02 '23 14:03 matthieusieben