NodeJS domain module does not fail

I experimented with domains in node JS. The code does what I expect, but when the code completes, the node does not fail, which makes me think that there is some irrevocable garbage or something that I do not understand. Any suggestions? Here is an example below:

var https = require('http'); var domain = require('domain'); function getUrl(aHost, aPath, okCallback) { var options = { host : aHost, path : aPath, method : 'GET' } , responseBody = '' , request = https.request(options, handleRequest); request.end(); function handleRequest(httpsResponse) { httpsResponse.setEncoding('utf8'); httpsResponse.on('data', function(chunk) { responseBody += chunk; }); httpsResponse.on('end', function(){ okCallback(httpsResponse.statusCode, responseBody); }); } } function fetch(aHost, aPath, okCallback) { var d = domain.create(); d.on('error', function(err) { console.log('It all went awry: ' + err.toString()); d.dispose(); }); d.run(function() { getUrl(aHost, aPath, d.bind(storeResult)); }); function storeResult(statusCode, responseBody) { if (statusCode >= 400) { throw new Error("Oops"); } else { okCallback(responseBody); } } } // works fine and exits cleanly with a valid path (like '/') fetch('www.google.com', '/invalidPath', function(body) { console.log('Received: ' + body); }); 
+4
source share

All Articles