diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c915830..12b81e1d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 `_` diff --git a/README.md b/README.md index 01037775..472384cb 100644 --- a/README.md +++ b/README.md @@ -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. + + + +```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 diff --git a/docs/plugins.md b/docs/plugins.md index 42ad32ac..a1c62bce 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -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 { diff --git a/types/plugins/shape.d.ts b/types/plugins/shape.d.ts index e1f131ae..e06a79bc 100644 --- a/types/plugins/shape.d.ts +++ b/types/plugins/shape.d.ts @@ -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 {