|
1 | 1 | [](https://www.npmjs.com/package/on-process-error) [](https://github.com/autoserver-org/on-process-error/graphs/contributors) [](https://www.apache.org/licenses/LICENSE-2.0) [](https://www.npmjs.com/package/on-process-error) [](#) [](https://standardjs.com) [](https://github.com/autoserver-org/eslint-config-standard-prettier-fp) |
| 2 | + |
| 3 | +Add an event listener to handle any process errors: |
| 4 | + |
| 5 | +- [`uncaughtException`](https://nodejs.org/api/process.html#process_event_uncaughtexception): an exception was thrown and not caught |
| 6 | +- [`unhandledRejection`](https://nodejs.org/api/process.html#process_event_unhandledrejection): a promise was rejected and not handled |
| 7 | +- [`rejectionHandled`](https://nodejs.org/api/process.html#process_event_rejectionhandled): a promise was rejected and handled too late |
| 8 | +- [`multipleResolves`](https://nodejs.org/api/process.html#process_event_multipleresolves): a promise was resolved/rejected twice |
| 9 | +- [`warning`](https://nodejs.org/api/process.html#process_event_warning): a warning was produced using [`process.emitWarning()`](https://nodejs.org/api/process.html#process_process_emitwarning_warning_options) |
| 10 | + |
| 11 | +# Usage |
| 12 | + |
| 13 | +<!-- eslint-disable no-unused-vars, node/no-missing-require, |
| 14 | +import/no-unresolved, unicorn/filename-case, strict --> |
| 15 | + |
| 16 | +```js |
| 17 | +const onProcessError = require('on-process-error') |
| 18 | + |
| 19 | +const undoSetup = onProcessError.setup() |
| 20 | +``` |
| 21 | + |
| 22 | +When any process errors occur, it will be logged using `console.error()`: |
| 23 | + |
| 24 | +- the message will include detailed information about the error |
| 25 | +- for `warning`, `console.warn()` will be used instead. |
| 26 | +- for `uncaughtException`, [`process.exit(1)` will be called after |
| 27 | + `console.error()`](https://nodejs.org/api/process.html#process_warning_using_uncaughtexception_correctly). |
| 28 | + |
| 29 | +You can undo everything by firing the function returned by |
| 30 | +`onProcessError.setup()` (called `undoSetup` in the example above). |
| 31 | + |
| 32 | +# Custom handling |
| 33 | + |
| 34 | +You can override the default behavior by passing a custom function instead. |
| 35 | + |
| 36 | +<!-- eslint-disable no-empty-function, no-unused-vars, node/no-missing-require, |
| 37 | +import/no-unresolved, unicorn/filename-case, strict --> |
| 38 | + |
| 39 | +```js |
| 40 | +const onProcessError = require('on-process-error') |
| 41 | + |
| 42 | +const undoSetup = onProcessError.setup( |
| 43 | + ({ eventName, promiseState, promiseValue, error, message }) => {}, |
| 44 | +) |
| 45 | +``` |
| 46 | + |
| 47 | +The function's argument is an object with the following properties: |
| 48 | + |
| 49 | +- `eventName` `{string}`: can be `uncaughtException`, `unhandledRejection`, |
| 50 | + `rejectionHandled`, `multipleResolves` or `warning` |
| 51 | +- `promiseState` `{string}`: whether promise was `resolved` or `rejected`. |
| 52 | + For `unhandledRejection`, `rejectionHandled` and `multipleResolves`. |
| 53 | +- `promiseValue` `{any}`: value resolved/rejected by the promise. |
| 54 | + For `unhandledRejection`, `rejectionHandled` and `multipleResolves`. |
| 55 | +- `error` `{error}`: |
| 56 | + - can be: |
| 57 | + - thrown by `uncaughtException` |
| 58 | + - emitted by `warning`. [`error.name`, `error.code` and `error.detail`](https://nodejs.org/api/process.html#process_event_warning) |
| 59 | + might be defined. |
| 60 | + - rejected by `unhandledRejection`, `rejectionHandled` or |
| 61 | + `multipleResolves`'s promise (if the promise was rejected). |
| 62 | + - if the error is not an `Error` instance (e.g. if it is a string), it will |
| 63 | + be normalized to one using `new Error()`. |
| 64 | +- `message` `{string}`: detailed message summing up all of the above. |
0 commit comments