Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node Exception Handling

What is the best way in node to handle unhandled expections that are coming out of core node code? I have a background process that runs and crawls web content and will run for long periods of time without issue, but every so often an unexpected exception occurs and I can't seem to gracefully handle it. The usual culprit appears to be some networking issue (lost connectivity) where the http calls I'm making fail. All of the functions that I have created follow the pattern of FUNCTION_NAME(error, returned_data), but in the situations where the error occurs I'm not seeing any of the functions I created in the call stack that is printed out, instead its showing some of the core node modules. I'm not really worried about these infrequent errors and their root cause, the purpose of this posting is just trying to find a graceful way of handling these exceptions.

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

Thanks

Chris

UPDATED TO ADD STACK

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)
like image 778
Chris Dellinger Avatar asked Mar 05 '12 18:03

Chris Dellinger


People also ask

Which module is required for exception handling in node?

Node. js domain module is used to intercept unhandled error. These unhandled error can be intercepted using internal binding or external binding. If errors are not handled at all then they will simply crash the Node application.

How do I create an exception in node?

js Throw Exception. However, you can also throw an error yourself: throw new Error('Throw makes it go boom!

What do you mean by exception handling?

Exception handling is the process of responding to unwanted or unexpected events when a computer program runs. Exception handling deals with these events to avoid the program or system crashing, and without this process, exceptions would disrupt the normal operation of a program.


1 Answers

is there some way to globally capture all unhandled exceptions?

You can catch all exceptions using process.on('uncaughtException'). Listening to this event will avoid the default action of printing the stack and exiting. However be conscious that ignoring exceptions may lead to problems in your app execution.

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

Pay attention to the documentation advice:

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

like image 127
seppo0010 Avatar answered Oct 21 '22 14:10

seppo0010