Node Exception Handling

What is the best way in node to handle raw expectations coming from node core code? I have a background process that launches and crawls web content and will run for long periods of time without problems, but every time an unexpected exception occurs, and I cannot gracefully handle it. The usual culprit seems to be a network problem (lost connection) where the http calls I make fail. All the functions that I created correspond to the FUNCTION_NAME (error, return_data) pattern, but in situations where an error occurs, I do not see any of the functions that I created in the call stack that are printed, instead some of the main node modules are displayed . I'm not really worried about these infrequent errors and their root cause, the purpose of this post is simply to find an elegant way to handle these exceptions.

I tried putting try / catch at the top level of my code, where everything works, but doesn't seem to catch these exceptions. Is it good practice in node to use try / catch in all lower level functions that use any kernel code? Or is there some way to globally catch all unhandled exceptions?

thanks

Chris

UPDATED ADD GLASS

node.js:201 throw e; // process.nextTick error, or 'error' event on first tick ^ Error: connect Unknown system errno 10060 at errnoException (net.js:642:11) at Object.afterConnect [as oncomplete] (net.js:633:18) 
+6
source share
4 answers

Is there a way to globally catch all unhandled exceptions?

You can catch all exceptions using process.on ('uncaughtException'). Listening to this event avoids the default action for printing the stack and exiting. However, be sure that ignoring exceptions can lead to problems running your application.

Link: http://nodejs.org/docs/latest/api/process.html#process_event_uncaughtexception

Pay attention to the documentation recommendations:

Note that uncaughtException is a very crude exception handling mechanism. Using try / catch in your program will give you more control over the flow of your program. Especially for server programs that are designed to work forever, uncaughtException can be a useful security mechanism.

+4
source

To catch network errors and avoid the default behavior (print and exit stack), you must listen for "error" events.

for instance

 var net = require('net'); var client = net.connect(80, 'invalid.host', function () { console.log("Worked"); }) client.on('error', console.log); 
+1
source

I recently wrote about this at http://snmaynard.com/2012/12/21/node-error-handling/ . The new node function in version 0.8 is domains and allows you to combine all forms of error handling into one simplified form of management. You can read about them in my post and in docs .

You can use domains to handle callback error arguments, error event emitters, and exceptions in one place. The problem in this particular case is that when you do not handle the error event emitter, node will print the stack trace by default and exit the application.

+1
source

I have compiled an error handling file that logs and sends me email messages whenever an unhandled exception is thrown. he then (optionally) tries to restart the server.

Check this!

0
source

Source: https://habr.com/ru/post/910016/


All Articles