Skip to content

Instantly share code, notes, and snippets.

@isaacs
Created May 9, 2012 00:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacs/2640679 to your computer and use it in GitHub Desktop.
Save isaacs/2640679 to your computer and use it in GitHub Desktop.
var EE = require('events').EventEmitter;
EE.prototype.emit = (function(orig) {
return function() {
if (this.listeners('error').length === 0) {
console.error(this.constructor.name, this);
throw new Error('EE without an error listener');
}
return orig.apply(this, arguments);
};
})(EE.prototype.emit);
var http = require('http');
var server = http.createServer(function(req, res) {
req.on('error', function(er) {
console.error('error on server request', er);
});
res.on('error', function(er) {
console.error('error on server response', er);
});
res.writeHead(200);
res.write('ready?\n');
setTimeout(function() {
res.end('go!\n');
server.close();
}, 100);
});
server.listen(1337, function() {
var req = http.request({ host: 'localhost',
port: 1337,
path: '/',
method: 'GET' });
req.on('error', function(er) {
console.error('error on client request', er);
});
req.on('response', function(res) {
res.on('error', function(er) {
console.error('error on client response', er);
});
res.pipe(process.stdout);
});
req.end();
});
/*
$ ./node must-have-errors.js
Agent { options: {},
requests: {},
sockets: {},
maxSockets: 5,
_events: { error: [] } }
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: EE without an error listener
at Agent.emit (/Users/isaacs/dev/js/node-v0.6/must-have-errors.js:7:13)
at Agent.<anonymous> (events.js:108:8)
at new Agent (http.js:914:8)
at http.js:1002:19
at NativeModule.compile (node.js:546:5)
at Function.require (node.js:514:18)
at Function._load (module.js:296:25)
at Module.require (module.js:354:17)
at require (module.js:370:17)
at Object.<anonymous> (/Users/isaacs/dev/js/node-v0.6/must-have-errors.js:13:12)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment