Reputation: 4826
In normal Javascript in the Browser Google Chrome will display an error in the console when you try to use an undefined variable. However in Node.js no error is displayed and I think the function that is using the undefined variable exits.
I've added:
process.on('uncaughtException', function(err) {
console.log( 'uncaughtException :', err );
});
but this isn't executed.
I've also added try-catch in the calling function and no exception is raised there.
And I'm using: "use strict";
So my question is, is there some way I can get/see an error when an undefined variable is accessed?
Upvotes: 1
Views: 2762
Reputation: 4826
The reason I'm not seeing this exception is other exception handler code is getting in before node. See my replies above re. the MongoDB driver.
Any other suggestions welcome. This is the code that is catching the exception:
catch (err) {
var errorObject = {err:"socketHandler", trace:err, bin:self.buffer, parseState:{
sizeOfMessage:self.sizeOfMessage,
bytesRead:self.bytesRead,
stubBuffer:self.stubBuffer}};
if(self.logger != null && self.logger.doError) self.logger.error("parseError", errorObject);
// We got a parse Error fire it off then keep going
self.emit("parseError", errorObject, self);
}
Neville
Upvotes: 0
Reputation: 112827
Node will do this automatically.
// test.js
"use strict";
function badFunction() {
return iDontExist + 3;
}
badFunction();
Then:
C:\Users\Domenic\Programming>node test.js
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
ReferenceError: iDontExist is not defined
at badFunction (C:\Users\Domenic\Programming\test.js:4:12)
at Object.<anonymous> (C:\Users\Domenic\Programming\test.js:7:1)
at Module._compile (module.js:444:26)
at Object..js (module.js:462:10)
at Module.load (module.js:351:31)
at Function._load (module.js:310:12)
at Array.0 (module.js:482:10)
at EventEmitter._tickCallback (node.js:192:40)
Upvotes: 1