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') {...}
?
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 ie 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.
Hope this helps you!
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
Contrary to your assumption, the single thread paradigm of node.js cause these types of error. In a multi-thread environment like java, all the called functions are executed inside the caller function:
java -> A -> B -> C -> D
So if the A() is wrapped in a try and catch all the internal exceptions are caught.
But in Async environment, the callback function is executed outside the caller function:
node -> A -> B(C)
node -> I -> C -> D
Here the function A call async function B (Usually a library) with callback function C as an argument, function B start an async task after it's finished node.js call function I which is an internal function of that library and it calls function C. Here you see I , C and D are called outside of your code.
So there are two things to consider here:
1- you must wrap your callback function code in try and catch.
2- there may be an exception in function I which you can not catch in your code. These are those exceptions that you are referring which none of the javascript files in the stack trace are yours because it's not initiated in your code.
Now if the function B (its library) is good written it must provide some means to catch those exceptions. One of them would be on('error', cb)
. So you should always check the library documentation to see how you can catch and handle such exceptions.
If the library is poorly written and nothing is provided to catch its exceptions in your code, you can use a debugger like chrome inspector or WebStorm debugger to catch them but there is no other way than manipulating its source code so at least they are bypassed to your code, or you can file a bug report.
Node.js also provide an uncaught exception handler which catches all uncaught exceptions:
process.on('uncaughtException', (err) => {
process.exit(1);
});
although it's risky to continue execution after here because things could be in an uncertain state, but it's a good place to log errors.
链接地址: http://www.djcxy.com/p/52654.html