node/test/fixtures/uncaught-exceptions/uncaught-monitor2.js
Gerhard Stoebich f4797ff1ef process: allow monitoring uncaughtException
Installing an uncaughtException listener has a side effect that process
is not aborted. This is quite bad for monitoring/logging tools which
tend to be interested in errors but don't want to cause side effects
like swallow an exception or change the output on console.

There are some workarounds in the wild like monkey patching emit or
rethrow in the exception if monitoring tool detects that it is the only
listener but this is error prone and risky.

This PR allows to install a listener to monitor uncaughtException
without the side effect to consider the exception has handled.

PR-URL: https://github.com/nodejs/node/pull/31257
Refs: https://github.com/nodejs/node/pull/30932
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
2020-01-10 18:35:01 -08:00

12 lines
240 B
JavaScript

'use strict';
// Keep the event loop alive.
setTimeout(() => {}, 1e6);
process.on('uncaughtExceptionMonitor', (err) => {
console.log(`Monitored: ${err.message}, will throw now`);
missingFunction();
});
throw new Error('Shall exit');