please-upgrade-node icon indicating copy to clipboard operation
please-upgrade-node copied to clipboard

Throw an error instead of using process.exit

Open aoberoi opened this issue 6 years ago • 2 comments

I'd like to suggest that instead of calling process.exit(), we throw an Error.

As described in node's documentation for process.exit(), using this method is problematic because it disrupts all asynchronous work in progress. The documentation recommends using an error instead:

If it is necessary to terminate the Node.js process due to an error condition, throwing an uncaught error and allowing the process to terminate accordingly is safer than calling process.exit().

One concern with making this change would be preserving the functionality of opts.exitCode. I believe we can set process.exitCode right before throwing the error, and it should preserve the intended behavior.

If the maintainers are happy with this idea, I can contribute the change in a PR.

aoberoi avatar May 14 '19 23:05 aoberoi

Another advantage: this would also give users of this package to catch the thrown error, in cases where they want some behavior other than exiting.

aoberoi avatar May 14 '19 23:05 aoberoi

We can use this to achieve!

process.on("uncaughtException", (err) => {
  console.error("Uncaught Exception:", err);
  // Don't call process.exit() to keep the server running
});

NITHISH-1609 avatar Aug 21 '23 19:08 NITHISH-1609