I've got the following:
var http = require('http');
server = http.createServer(function(request, response) {
try {
// Register a callback: this happens on a new stack on some later tick
request.on('end', function() {
throw new Error; // Not caught
})
throw new Error; // Caught below
}
catch (e) {
// Handle logic
}
}
Now, the first Error
gets caught in the try...catch
, but the second Error
does not appear to be getting caught.
A couple questions:
Error
not get caught because it occurs on a different stack? If so, am I to understand that try...catch
behavior is not lexically bound, but depends instead on the current stack? Am I interpreting this correctly?You could also use a simple EventEmitter
to handle an error in a case like this.
var http = require('http');
var EventEmitter = require("events").EventEmitter;
server = http.createServer(function(request, response) {
var errorHandler = new EventEmitter;
// Register a callback: this happens on a new stack on some later tick
request.on('end', function() {
if(someCondition) {
errorHandler.emit("error", new Error("Something went terribly wrong"));
}
});
errorHandler.on("error", function(err) {
// tell me what the trouble is....
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With