Am working on an application where several types of errors can be thrown. For each different type of error, the node process must terminate with a different exit code.
At the moment I am throwing errors like:
throw new Error1('Failed, because of reason 1');
throw new Error2('Failed, because of reason 2');
However this causes the process to exit when the error is thrown with exit code 8 every time.
What I need is an elegant way to throw specific exit codes depending on which error is thrown. I.e. when I throw Error1, it should exit with code 1 and when I throw Error2 it should exit with code 2.
At the moment it just exits with code '8' every time.
I have looked at other articles and there are suggestions that using process.on('uncaughtException', ...); will work; and it does, however it requires 'catching' the error, re printing it out and then exiting.
Any suggestions?
Many thanks.