D
D
Danil2015-05-26 10:34:56
Node.js
Danil, 2015-05-26 10:34:56

How to reconfigure errors in nodejs?

I didn’t know how to formulate the title correctly, in general, at the moment when an error occurs, the application immediately crashes. How to make it so that in case of errors, for example, a certain function works and the application continues to work? The issue of production worries, I would not want the site to fall offline at the first error found.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Avilov, 2015-05-26
@TheCreator

You need this kind of magic, but it's not good to do it this way.

process.on('uncaughtException', function (processError) {
  console.log(processError.stack);
});

Just paste at the beginning of the script. Now the application will continue to work, even if some error occurs, it simply logs it to the console. But, again, it's not nice to do so.
It might be better to look for a process manager like forever, which will watch the process and if it crashes, restart it. So much more kosher, actually. Well, in general, production should not fall in principle.

V
Vladislav, 2015-05-26
@externuz

Give a specific example of a method in which you need to catch an error

T
Timur Shemsedinov, 2015-05-26
@MarcusAurelius

On the one hand, leaving a process in memory after an unhandled error is bad; there will be memory leaks, corrupted data, and incomplete handlers that can lead to unpredictable consequences. In this case, many recommend restarting the process using forever managers and see nothing wrong with this. I think it's not normal when the process constantly crashes and restarts. You still need to look for errors to correct them. Nevertheless, rare errors can, of course, end up in production. Here you can use several levels of error recovery: domains, which have already been mentioned - for catching asynchronous exceptions, sandboxes (sandbox) - for screening the memory area in which a leak occurs in the application code with the removal of sandboxes after critical exceptions, critical sections - they are in the node are rarely needed, because not suitable for asynchronous code, but still sometimes useful. I used all these methods in combination in the Impress application server, if you don’t take it directly, then it won’t hurt to look at the source:https://www.npmjs.com/package/impress

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question