diff --git a/doc/api/process.md b/doc/api/process.md index c868f01fa1d3e8..cf118b876d5a46 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -1177,6 +1177,44 @@ a code. Specifying a code to [`process.exit(code)`][`process.exit()`] will override any previous setting of `process.exitCode`. +## process.exitWithException(error[, fromPromise]) + + +* `error` {any} +* `fromPromise` {boolean} + +Exit with the provided error value as if it was thrown from its original +context. This allows an error to re-enter Node.js's error logic unmodified, +and will use the default formatted error printing. + +If an error stack is available on the value, this method of exiting does not add +additional context, unlike re-throwing using [`throw`][]. + +This method is particularly useful when listening to the +[`'unhandledRejection'`][] process event. + +As an example: +```js +process.on('unhandledRejection', (err) => { + process.exitWithException(err); +}); + +Promise.reject(new Error('uh oh')); +``` + +Outputs something similar to the following, as is often desirable: +```console +throw.js:5 +Promise.reject(new Error('uh oh')); + ^ + +Error: uh oh + at Object. (throw.js:5:16) + +``` + ## process.getegid()