Skip to content

Commit

Permalink
Document internal props
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Nov 20, 2022
1 parent a6908da commit aabb86e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Features

- Error [`props`](README.md#%EF%B8%8F-error-properties) now preserve
[property descriptors](https://github.com/ehmicky/redefine-property)
- Error [`props`](README.md#%EF%B8%8F-error-properties) can be marked as
[non-enumerable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties)
by [prefixing their name](README.md#internal-error-properties) with `_`
- [`plugin.properties()`](docs/plugins.md#properties) can now also return
non-enumerable properties by prefixing their name with `_`

Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,25 @@ const error = new InputError('...', { props: { isUserError: true } })
console.log(error.isUserError) // true
```

### Internal error properties

Error properties that are internal or secret can be prefixed with `_`. This
makes them
[non-enumerable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties),
which prevents iterating or logging them.

<!-- eslint-disable no-underscore-dangle -->

```js
const error = new InputError('...', {
props: { userId: 6, _isUserError: true },
})
console.log(error.userId) // 6
console.log(error._isUserError) // true
console.log(Object.keys(error)) // ['userId']
console.log(error) // `userId` is logged, but not `_isUserError`
```

## 🎀 Wrap errors

### Throw errors
Expand Down
5 changes: 3 additions & 2 deletions docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,10 @@ _Type_: `(info) => object`
Set properties on `error.*` (including `message` or `stack`). The properties to
set must be returned as an object.

If a property's name starts with `_`, it is marked as
Error properties that are internal or secret can be prefixed with `_`. This
makes them
[non-enumerable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties),
i.e. it can neither be iterated nor logged.
which prevents iterating or logging them.

```js
export default {
Expand Down
5 changes: 5 additions & 0 deletions types/plugins/shape.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ export interface Plugin {
* Set properties on `error.*` (including `message` or `stack`).
* The properties to set must be returned as an object.
*
* Error properties that are internal or secret can be prefixed with `_`.
* This makes them
* [non-enumerable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties),
* which prevents iterating or logging them.
*
* @example
* ```js
* export default {
Expand Down

0 comments on commit aabb86e

Please sign in to comment.