What is difference between method process.exit(1) and process.exit(0) in node.js? - node.js

In node.js applications i saw usage of both these methods process.exit(1) and process.exit(0). Can anybody give me the exact answer ?

Node normally exits with a 0 status code when no more async operations
are pending. There are other exit codes which are described below:
1 - Uncaught Fatal Exception: There was an uncaught exception, and it was not handled by a domain or an uncaughtException event handler.
2 - Unused: Reserved by Bash for built in misuse.
3 - Internal JavaScript Parse Error: The JavaScript source code internal in Node's bootstrapping process caused a parse error. This is
extremely rare, and generally can only happen during the development
of Node itself.
4 - Internal JavaScript Evaluation Failure: The JavaScript source code internal in Node's bootstrapping process failed to return a
function value when evaluated. This is extremely rare, and generally
can only happen during the development of Node itself.
5 - Fatal Error: There was a fatal unrecoverable error in V8. Typically, a message will be printed to stderr with the prefix FATAL
ERROR.
6 - Non-function Internal Exception Handler: There was an uncaught exception, but the internal fatal exception handler function was
somehow set to a non-function, and could not be called.
7 - Internal Exception Handler Run-Time Failure: There was an uncaught exception, and the internal fatal exception handler function
itself threw an error while attempting to handle it.
8 - Unused
9 - Invalid Argument: Either an unknown option was specified, or an option requiring a value was provided without a value.
10 - Internal JavaScript Run-Time Failure: The JavaScript source code internal in Node's bootstrapping process threw an error when the
bootstrapping function was called. This is extremely rare, and
generally can only happen during the development of Node itself.
11 - Invalid Debug Argument: The --debug and/or --debug-brk options were set, but an invalid port number was chosen
>128 - Signal Exits: If Node receives a fatal signal such as SIGKILL or SIGHUP, then its exit code will be 128 plus the value
of the signal code. This is a standard Unix practice, since exit codes
are defined to be 7-bit integers, and signal exits set the high-order
bit, and then contain the value of the signal code.
Source: https://www.tutorialspoint.com/nodejs/nodejs_process.htm

You can find the answer to your question in the documentation: https://nodejs.org/api/process.html#process_process_exit_code
Basically if you want to exit with success use 0 if you want to exit with failure use 1.

0 is a success code and 1 (or another number) can be a failure code. 0 will be used if nothing is specified. Useful for passing information on the way out. Answered on SO here:
https://stackoverflow.com/a/5266239/5463636
More info direct from the Node.js docs here:
https://nodejs.org/api/process.html#process_process_exit_code

Related

Why does NodeJS terminate execution without throwing an exception?

Adding the console.log in this recursive function prevents NodeJS from throwing a "Maximum call stack size exceeded" exception. Instead it just exits after a couple thousand iterations with no message (using Node v16.6.0 on Win10).
Why does the console.log change the exception throwing behavior, and how should I catch this exception without removing the console.log?
(In browsers it also throws an exception as I expected.)
function recursive(i) {
console.log(i)
return recursive(i + 1) * 2
}
try {
recursive(0)
} catch (ex) {
console.log(ex)
}
There is no expected exception here.
Semantically, this is an infinite loop, which is valid. If the JS engine doesn't do tail call optimization infinite recursion (like your example) will cause the call stack to overflow, but this is neither required nor desired. The ECMAScript standard even states how tail calls should be optimized, though this isn't implemented in most JS engines (except for JavaScriptCore used in Safari/WebKit). Whether Node emits the Maximum call stack size exceeded exception or not is nothing you should rely on.

What's the difference between abort() and exit() in Node?

I noticed that both process.exit() and process.abort() both stop a script. What are the differences between the two besides that one logs Aborted?
process.abort() stops the process immediately.
process.exit([exitCode]) method instructs Node.js to terminate the process as quickly as possible. You can also specify an exit code.
For exit codes:
0 means the process was exited successfully.
1 means it ended abnormally.
When omitted, 0 is the default value.
Calling process.exit() will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr.
The proper recommended way to exit from process is to set the exit code and allow process to exit naturally because calling process.exit() forces the process to exit before any additional writes to stdout can be performed.
process.exitCode = 1;
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().
The process.abort() method causes the Node.js process to exit immediately and generate a core file.
The process.exit() method instructs Node.js to terminate the process as quickly as possible with the specified exit code.
If the code is 0 mean exists as success case.
if the code is 1 mean exists as failer case.
And
The process.abort() method causes the Node.js process to exit immediately and generate a core file.
Nothing more difference I have seen except those
abort - this results in the abnormal termination of the process.
exit - function causes normal process termination

How to catch SIGABRT inside a nodejs process?

I do hundreds of request per second using the request module in nodejs and sometimes i get the following error
nodejs: ../deps/uv/src/unix/async.c:149: uv__async_io: Assertion `n == sizeof(val)' failed.
Code: null Signal: SIGABRT
how can i catch the signal?
An assert is blowing up, and that particular one is there to catch programming errors, or totally unexpected conditions. Another user reported this to us, but it turned out it was a missuse of the API. I suggest you open an issue on our bugtracker: https://github.com/libuv/libuv/issues, ideally with a reproducible reduced test case.

MCH1206 floating point overflow condition

IBM OS400 C ILE program
Does anyone know when MCH1206 is issued in a C program? The C program does not have any error handling defined.
In my environment, whenever there is an overflow, - e.g. result = pow(a,b)
If there is an overflow, result is set to HUGE_VAL, and it continues execution. In the joblog, i can see this message:
"Floating-point overflow exception occurred in CEESDXPD."
However, in another environment, it stops execution and issuing this message
"Floating point overflow condition detected.
Application error. MCH1206 unmonitored by F07RTB at statement 00000038,"
Is there are global handling (e.g. defined in the system level)? Global handling for maths library?
Thanks

Node.js error handling guarantee with net.createConnection

I was wondering if Node.js provides any guarantee that the error handler will catch all errors in code like the following:
var c = net.createConnection(port, host);
c.on('error', function (e) { ... });
I've had to deal with code where other statements intervened between the call to createConnection and the statement which sets the error handler, and this sometimes caused errors internal to createConnection to percolate to the top level (presumably because these errors sometimes occurred before the error handler had been set). But unless I'm missing something, this could in principle happen in the code above -- it's just not very likely. Is there any way of actually guaranteeing that all errors which occur within createConnection will get passed to the error handler?
It will only happen if there are bugs in node.
JavaScript is single threaded, the process will not normally be interrupted between attaching the event handler and creating the connection.
No error generated in javascript land will be uncaught by that code.
So if there was a bug in node, there could probably be some errors your code would not catch. If you had some dodgy low level code running in the background causing mayhem it could be possible that some errors would occur that your code would not catch.
But under normal usage, your code is "threadsafe". Your binding the event handler before any errors occur because your code is blocking and nothing can throw errors.

Resources