Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
process.on('error', function(er) {
console.error('error on process', er);
});
process.stdout.on('error', function(er) {
console.error('error on stdout', er);
});
process.stderr.on('error', function(er) {
// hah, this will probably fail...
console.error('error on stderr', er);
});
var EE = require('events').EventEmitter;
EE.prototype.emit = (function(orig) {
return function() {
if (this.listeners('error').length === 0 && arguments[0] !== 'newListener') {
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');
http.globalAgent.on('error', function(er) {
console.error('error on agent', er);
});
console.error('global agent error listeners', http.globalAgent.listeners('error'));
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.on('error', function(er) {
console.error('error on server', er);
});
server.listen(1337, function() {
var req = http.request({ host: 'localhost',
port: 1337,
path: '/',
agent: http.globalAgent,
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
global agent error listeners [ [Function] ]
ready?
go!
Socket { _handle:
{ writeQueueSize: 0,
socket: [Circular],
onread: [Function: onread] },
_pendingWriteReqs: 0,
_flags: 0,
_connectQueueSize: 0,
destroyed: false,
errorEmitted: false,
bytesRead: 97,
bytesWritten: 64,
allowHalfOpen: undefined,
_connecting: false,
writable: true,
_events:
{ free: [Function],
close: [ [Function] ],
agentRemove: [Function],
error: [] },
_httpMessage:
{ output: [],
outputEncodings: [],
writable: true,
_last: true,
chunkedEncoding: false,
shouldKeepAlive: true,
useChunkedEncodingByDefault: false,
_hasBody: true,
_trailer: '',
finished: true,
agent:
{ options: {},
requests: {},
sockets: [Object],
maxSockets: 5,
_events: [Object],
createConnection: [Function] },
socketPath: undefined,
method: 'GET',
path: '/',
_headers: { host: 'localhost:1337' },
_headerNames: { host: 'Host' },
_events: { error: [Object], response: [Function] },
_header: 'GET / HTTP/1.1\r\nHost: localhost:1337\r\nConnection: keep-alive\r\n\r\n',
_headerSent: true,
socket: [Circular],
connection: [Circular],
parser:
{ _headers: [],
_url: '',
onHeaders: [Function: parserOnHeaders],
onHeadersComplete: [Function: parserOnHeadersComplete],
onBody: [Function: parserOnBody],
onMessageComplete: [Function: parserOnMessageComplete],
socket: [Circular],
incoming: [Object],
onIncoming: [Function] },
res:
{ socket: [Circular],
connection: [Circular],
httpVersion: '1.1',
complete: true,
headers: [Object],
trailers: {},
readable: false,
url: '',
method: null,
statusCode: 200,
client: [Circular],
httpVersionMajor: 1,
httpVersionMinor: 1,
upgrade: false,
_events: [Object] } },
ondrain: [Function],
ondata: [Function],
onend: [Function],
_connectQueue: null,
readable: true }
/Users/isaacs/dev/js/node-v0.6/must-have-errors.js:19
throw new Error('EE without an error listener');
^
Error: EE without an error listener
at Socket.emit (/Users/isaacs/dev/js/node-v0.6/must-have-errors.js:19:13)
at IncomingMessage.<anonymous> (http.js:1303:18)
at IncomingMessage.<anonymous> (events.js:88:20)
at IncomingMessage.emit (/Users/isaacs/dev/js/node-v0.6/must-have-errors.js:21:17)
at HTTPParser.parserOnMessageComplete [as onMessageComplete] (http.js:127:21)
at Socket.ondata (http.js:1179:24)
at TCP.onread (net.js:374:27)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.