title: How do I debug node.js errors when my code is nowhere in the stack trace?
body: <p>And actually, I don't fully understand <em>why</em> 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?</p>
  
  <p>I'm writing a pretty simple proxy server using node/express.  As an example, I was periodically getting this "socket hangup error":</p>
  
  <pre><code>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' }
  </code></pre>
  
  <p>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.</p>
  
  <p>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 <code>request.on('error') {...}</code>?</p>