https://github.com/ehmicky/set-error-props
Properly update an error's properties
https://github.com/ehmicky/set-error-props
browser code-quality deep deepmerge error error-classes error-handler error-handling error-monitoring error-reporting errors exceptions javascript library merge message monitoring nodejs properties typescript
Last synced: 3 months ago
JSON representation
Properly update an error's properties
- Host: GitHub
- URL: https://github.com/ehmicky/set-error-props
- Owner: ehmicky
- License: mit
- Created: 2022-08-26T17:53:05.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2025-03-13T05:04:08.000Z (3 months ago)
- Last Synced: 2025-03-13T06:19:09.642Z (3 months ago)
- Topics: browser, code-quality, deep, deepmerge, error, error-classes, error-handler, error-handling, error-monitoring, error-reporting, errors, exceptions, javascript, library, merge, message, monitoring, nodejs, properties, typescript
- Language: JavaScript
- Homepage:
- Size: 7.68 MB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
[](https://www.npmjs.com/package/set-error-props)
[](https://unpkg.com/set-error-props?module)
[](/src/main.d.ts)
[](https://codecov.io/gh/ehmicky/set-error-props)
[](https://bundlephobia.com/package/set-error-props)
[](https://fosstodon.org/@ehmicky)
[](https://medium.com/@ehmicky)Properly update an error's properties.
# Hire me
Please
[reach out](https://www.linkedin.com/feed/update/urn:li:activity:7117265228068716545/)
if you're looking for a Node.js API or CLI engineer (11 years of experience).
Most recently I have been [Netlify Build](https://github.com/netlify/build)'s
and [Netlify Plugins](https://www.netlify.com/products/build/plugins/)'
technical lead for 2.5 years. I am available for full-time remote positions.# Features
- Prevents overriding [error core properties](#error-core-properties) (`name`,
`message`, etc.)
- Protects against [prototype pollution](#prototype-pollution)
- Prevents overriding [existing properties](#overriding-protection)
- [Copies](#error-copy) another error's properties
- Can set properties as [non-enumerable](#non-enumerable-properties)
- Preserves properties [descriptors](#descriptors) (`enumerable`, `writable`,
`configurable`, `get`/`set`)
- [Exception-safe](#exception-safety): this only throws syntax errors
- Strict [TypeScript typing](/src/main.d.ts) of the return value# Example
```js
import setErrorProps from 'set-error-props'const error = new Error('one')
setErrorProps(error, { prop: true, message: 'two' })
console.log(error.prop) // true
console.log(error.message) // 'one': message is readonly
```# Install
```bash
npm install set-error-props
```This package works in both Node.js >=18.18.0 and
[browsers](https://raw.githubusercontent.com/ehmicky/dev-tasks/main/src/browserslist).This is an ES module. It must be loaded using
[an `import` or `import()` statement](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c),
not `require()`. If TypeScript is used, it must be configured to
[output ES modules](https://www.typescriptlang.org/docs/handbook/esm-node.html),
not CommonJS.# API
## setErrorProps(error, props, options?)
`error` `Error | object`\
`props` `Error | object`\
`options` [`Options?`](#options)\
_Return value_: `Error`Assigns `props` to `error`, then returns `error`.
### Options
#### soft
_Type_: `boolean`\
_Default_: `false`Prevents [overriding](#overriding-protection) existing properties.
# Usage
## Error core properties
```js
const error = new Error('one')
setErrorProps(error, { message: 'two' })
console.log(error.message) // 'one'
```## Prototype pollution
```js
const error = new Error('one')
setErrorProps(error, { toString: () => 'injected' })
console.log(error.toString()) // 'Error: one'
console.log(Error.prototype.toString()) // 'Error'
```## Overriding protection
```js
const error = new Error('message')
error.one = true
setErrorProps(error, { one: false, two: true }, { soft: true })
console.log(error.one) // true
console.log(error.two) // true
```## Error copy
```js
const error = new Error('one')
const secondError = new Error('two')
secondError.prop = true
setErrorProps(error, secondError)
console.log(error.message) // 'one'
console.log(error.prop) // true
```## Non-enumerable properties
```js
const error = new Error('message')// Properties that start with `_` are not enumerable
setErrorProps(error, { _one: true, two: true })console.log(error._one) // true
console.log(error.two) // true
console.log(Object.keys(error)) // ['two']
console.log(error) // Prints `two` but not `_one`
```## Descriptors
```js
const error = new Error('message')
Object.defineProperty(error, 'prop', {
value: false,
enumerable: false,
writable: true,
configurable: true,
})
setErrorProps(error, { prop: true })
console.log(error.prop) // true
console.log(Object.getOwnPropertyDescriptor(error, 'prop').enumerable) // false
```## Exception safety
```js
const error = new Proxy(new Error('message'), {
set: () => {
throw new Error('example')
},
defineProperty: () => {
throw new Error('example')
},
})
setErrorProps(error, { prop: true }) // This does not throw
```# Related projects
- [`modern-errors`](https://github.com/ehmicky/modern-errors): Handle errors in
a simple, stable, consistent way
- [`error-custom-class`](https://github.com/ehmicky/error-custom-class): Create
one error class
- [`error-class-utils`](https://github.com/ehmicky/error-class-utils): Utilities
to properly create error classes
- [`error-serializer`](https://github.com/ehmicky/error-serializer): Convert
errors to/from plain objects
- [`normalize-exception`](https://github.com/ehmicky/normalize-exception):
Normalize exceptions/errors
- [`is-error-instance`](https://github.com/ehmicky/is-error-instance): Check if
a value is an `Error` instance
- [`set-error-class`](https://github.com/ehmicky/set-error-class): Properly
update an error's class
- [`set-error-message`](https://github.com/ehmicky/set-error-message): Properly
update an error's message
- [`wrap-error-message`](https://github.com/ehmicky/wrap-error-message):
Properly wrap an error's message
- [`set-error-stack`](https://github.com/ehmicky/set-error-stack): Properly
update an error's stack
- [`merge-error-cause`](https://github.com/ehmicky/merge-error-cause): Merge an
error with its `cause`
- [`error-cause-polyfill`](https://github.com/ehmicky/error-cause-polyfill):
Polyfill `error.cause`
- [`handle-cli-error`](https://github.com/ehmicky/handle-cli-error): 💣 Error
handler for CLI applications 💥
- [`log-process-errors`](https://github.com/ehmicky/log-process-errors): Show
some ❤ to Node.js process errors
- [`error-http-response`](https://github.com/ehmicky/error-http-response):
Create HTTP error responses
- [`winston-error-format`](https://github.com/ehmicky/winston-error-format): Log
errors with Winston# Support
For any question, _don't hesitate_ to [submit an issue on GitHub](../../issues).
Everyone is welcome regardless of personal background. We enforce a
[Code of conduct](CODE_OF_CONDUCT.md) in order to promote a positive and
inclusive environment.# Contributing
This project was made with ❤️. The simplest way to give back is by starring and
sharing it online.If the documentation is unclear or has a typo, please click on the page's `Edit`
button (pencil icon) and suggest a correction.If you would like to help us fix a bug or add a new feature, please check our
[guidelines](CONTRIBUTING.md). Pull requests are welcome!