hapijs / hapijs/boom

Add support for native `cause`

Open
#300 6 comments 0 reactions 0 assignees View on GitHub
feature
Dominant language
JavaScript
Stars
2.9k
Forks
191
PR merge metrics
No merged PRs in 30d

Description

#### 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:

```js
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:
```ts
export function badImplementation(message?: string, data?: Data, errorOptions?: { cause?: unknown }): Boom;
```

and specify that from now on, `data` should no longer be used to contain the originating error.
```ts
function sqlQuery(query, values) {
try {
// Execute some SQL
} catch (err) {
throw internal('Failed to query DB', { code: "sql-query-error", query, values }, { cause: err })
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.