Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I debug node.js errors when my code is nowhere in the stack trace?

And actually, I don't fully understand why my code is not in the stack trace, if node is single threaded. Maybe I'm fundamentally misunderstanding, something, but why does my application sometimes die with a stack trace that doesn't have anything I've written in it?

I'm writing a pretty simple proxy server using node/express. As an example, I was periodically getting this "socket hangup error":

Error: socket hang up
 at createHangUpError (_http_client.js:250:15)
 at Socket.socketOnEnd (_http_client.js:342:23)
 at emitNone (events.js:91:20)
 at Socket.emit (events.js:185:7)
 at endReadableNT (_stream_readable.js:926:12)
 at _combinedTickCallback (internal/process/next_tick.js:74:11)
 at process._tickCallback (internal/process/next_tick.js:98:9) code: 'ECONNRESET' }

And since none of the javascript files in the stack trace are mine, I had no idea where this was coming from. It was basically trial and error, trying to catch errors and adding .on style error-handlers until I found the right place.

I feel like I'm fundamentally missing something - what I should I be doing differently in order to debug errors like this? How do I know where to handle it if I can't see what (in my code) is causing it? How do I know whether I should be using a try/catch block, or something like request.on('error') {...}?

like image 441
Jer Avatar asked Jul 26 '16 19:07

Jer


People also ask

How do I debug Node js code in production?

Remote debugging and debugging remote machine. Live edit of running code, optionally persisting changes back to the file-system. Set breakpoints in files that are not loaded into V8 yet - useful for debugging module loading/initialization. Embeddable in other applications - see Embedding HOWTO for more details.

How do I enable debug mode in Node JS?

Open up Preferences > Settings and in the search box type in “node debug”. Under the Extensions tab there should be one extension titled “Node debug”. From here, click the first box: Debug > Node: Auto Attach and set the drop down to “on”. You're almost ready to go now.


2 Answers

Some errors, like the one mentioned by you, are not caused by your code. In fact it is caused by the absence of code in your application. (For example, your application code is probably missing the code for gracefully handling ECONNRESET i.e. remote socket disconnection.

Now, to your question about how to debug such errors (including third-party code). Of course, you can use stack-trace and longjohn etc.

But, for me, the easier & quicker solution is to run the application in debug mode with --inspect option, with Chrome debugger to inspect it (no breakpoints), with Pause on Exceptions option enabled. That's all you need to do. Now whenever there is an exception, chrome debugger will pause the application exactly at the line where the exception is thrown. Makes it a lot easier to find such bugs.

Pause On Exception

Hope this helps you!

like image 138
Santanu Biswas Avatar answered Oct 08 '22 07:10

Santanu Biswas


You could do something like for debugging such errors.

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

You could also increase your stack trace size limit and/or stack size.

node --stack_trace_limit=200 app.js //defaults to 10
node --stack-size=1024 app.js // defaults to 492kB
like image 5
Kaushik Wavhal Avatar answered Oct 08 '22 05:10

Kaushik Wavhal