Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
normalize-exception
Advanced tools
Normalize exceptions/errors.
In JavaScript, one can throw
any value including strings, objects (like
{ message: "..." }
) or even undefined
. This normalizes any exception
to an Error
instance.
It also fixes any missing or invalid error properties:
name
, message
, stack
, cause
, errors
.
import normalizeException from 'normalize-exception'
try {
throw null
} catch (error) {
const normalizedError = normalizeException(error)
console.log(normalizedError) // Error: null
// Without `normalizeException()`, this would throw
console.log(normalizedError.name)
}
console.log(normalizeException('message')) // Error: message
console.log(normalizeException({ name: 'TypeError', message: 'message' })) // TypeError: message
const error = new TypeError('message')
console.log(error.stack) // TypeError: message
// `error.stack` is cached, so it does not update
error.message += ' otherMessage'
console.log(error.stack) // TypeError: message
const normalizedError = normalizeException(error)
console.log(normalizedError.stack) // TypeError: message otherMessage
const error = new Error('message', { cause: 'innerError' })
console.log(error.cause instanceof Error) // false
const normalizedError = normalizeException(error)
console.log(normalizedError.cause instanceof Error) // true
console.log(normalizedError.cause) // Error: innerError
npm install normalize-exception
This package is an ES module and must be loaded using
an import
or import()
statement,
not require()
.
error
any
Return value: Error
normalizeException()
never throws.
If error
is an Error
instance, it is returned. Any missing or invalid error
property is directly modified.
If it is not an Error
instance, a new one is created and returned.
modern-errors
: Handle errors
like it's 2022 🔮error-type
: Create custom error
typesmerge-error-cause
: Merge an
error with its cause
error-cause-polyfill
:
Polyfill error.cause
For any question, don't hesitate to submit an issue on GitHub.
Everyone is welcome regardless of personal background. We enforce a Code of conduct in order to promote a positive and inclusive environment.
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. Pull requests are welcome!
1.2.0
Proxy
instances, by converting them to non-Proxy
get
functions that throwFAQs
Normalize exceptions/errors
The npm package normalize-exception receives a total of 12,728 weekly downloads. As such, normalize-exception popularity was classified as popular.
We found that normalize-exception demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.