Allow "unknown" values to be boomified
- 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): yes
* *is this issue affecting a production system?* (yes/no): yes
#### Context
* *node version*: *
* *module version*: 9.1.4
* *environment* (e.g. node, browser, native): *
* *used with* (e.g. hapi application, another framework, standalone, ...): Typescript >= 4.4
* *any other relevant information*: *
#### What problem are you trying to solve?
Since typescript 4.4, the type of the `err` in a catch block will be "unknown".
```ts
try {
// stuff
} catch (err) {
throw boomify(err) // <=== typescript error "Argument of type "unknown" cannot be assigned to type: Error"
}
```
There are two solutions, both of which result in a lot of added code
- First:
```ts
try {
// stuff
} catch (err) {
throw err instanceof Error ? boomify(err) : err
}
```
- Second:
```ts
// we have to import this everywhere when it is needed
const asError = (err: unknown): Error => err instanceof Error ? err : Object.assign(new Error(err?.message ?? 'Unknown error'), err)
try {
// stuff
} catch (err) {
throw boomify(asError(err))
}
```
Since `boomify` is already an error wrapper utility, having to do either of these is anoying.
#### Do you have a new or modified API suggestion to solve the problem?
Could we allow the first argument of `boomify()` to be of type `unknown` ?
If you don't want to change the current signature, we could also make this optional:
```ts
boomify(err as unknown, { allowUnknown: true })
```
Contributor guide
Assessment
This issue has not been assessed yet.