normalize-exception
Advanced tools
Comparing version 1.0.0 to 1.0.1
/** | ||
* Normalize exception/error. | ||
* | ||
* 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`. | ||
* | ||
* @example | ||
* ```js | ||
* 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 | ||
* ``` | ||
*/ | ||
export default function normalizeException(error: any): Error |
{ | ||
"name": "normalize-exception", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "exports": "./build/src/main.js", |
42465
263