Unhandled 'error' events kill program
By default 'error' throws. addListener to prevent this.
This commit is contained in:
parent
11f673e819
commit
6abdf051d3
@ -1,5 +1,44 @@
|
|||||||
exports.EventEmitter = process.EventEmitter;
|
exports.EventEmitter = process.EventEmitter;
|
||||||
|
|
||||||
|
|
||||||
|
process.EventEmitter.prototype.emit = function (type) {
|
||||||
|
if (type == 'error') {
|
||||||
|
if (!this._events || !this._events.error ||
|
||||||
|
(this._events.error instanceof Array && !this._events.error.length))
|
||||||
|
{
|
||||||
|
if (arguments[1] instanceof Error) {
|
||||||
|
throw arguments[1];
|
||||||
|
} else {
|
||||||
|
throw new Error("Uncaught, unspecfied 'error' event.");
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this._events) return false;
|
||||||
|
if (!this._events[type]) return false;
|
||||||
|
|
||||||
|
if (typeof this._events[type] == 'function') {
|
||||||
|
var args = Array.prototype.slice.call(arguments, 1);
|
||||||
|
|
||||||
|
this._events[type].apply(this, args);
|
||||||
|
return true;
|
||||||
|
|
||||||
|
} else if (this._events[type] instanceof Array) {
|
||||||
|
var args = Array.prototype.slice.call(arguments, 1);
|
||||||
|
|
||||||
|
|
||||||
|
var listeners = this._events[type].slice(0);
|
||||||
|
for (var i = 0, l = listeners.length; i < l; i++) {
|
||||||
|
listeners[i].apply(this, args);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// process.EventEmitter is defined in src/node_events.cc
|
// process.EventEmitter is defined in src/node_events.cc
|
||||||
// process.EventEmitter.prototype.emit() is also defined there.
|
// process.EventEmitter.prototype.emit() is also defined there.
|
||||||
process.EventEmitter.prototype.addListener = function (type, listener) {
|
process.EventEmitter.prototype.addListener = function (type, listener) {
|
||||||
@ -27,6 +66,7 @@ process.EventEmitter.prototype.addListener = function (type, listener) {
|
|||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
process.EventEmitter.prototype.removeListener = function (type, listener) {
|
process.EventEmitter.prototype.removeListener = function (type, listener) {
|
||||||
if ('function' !== typeof listener) {
|
if ('function' !== typeof listener) {
|
||||||
throw new Error('removeListener only takes instances of Function');
|
throw new Error('removeListener only takes instances of Function');
|
||||||
|
@ -548,9 +548,13 @@ function connectionListener (socket) {
|
|||||||
parser.socket = socket;
|
parser.socket = socket;
|
||||||
|
|
||||||
if (self.secure) {
|
if (self.secure) {
|
||||||
socket.setSecure(self.credentials);
|
socket.setSecure(self.credentials);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
socket.addListener('error', function (e) {
|
||||||
|
sys.puts('node http server: ' + e);
|
||||||
|
});
|
||||||
|
|
||||||
socket.ondata = function (d, start, end) {
|
socket.ondata = function (d, start, end) {
|
||||||
var bytesParsed = parser.execute(d, start, end - start);
|
var bytesParsed = parser.execute(d, start, end - start);
|
||||||
if (parser.incoming && parser.incoming.upgrade) {
|
if (parser.incoming && parser.incoming.upgrade) {
|
||||||
|
30
lib/net.js
30
lib/net.js
@ -826,20 +826,26 @@ Stream.prototype.destroy = function (exception) {
|
|||||||
|
|
||||||
|
|
||||||
Stream.prototype._shutdown = function () {
|
Stream.prototype._shutdown = function () {
|
||||||
if (this.writable) {
|
if (!this.writable) {
|
||||||
this.writable = false;
|
throw new Error('The connection is not writable');
|
||||||
|
} else {
|
||||||
|
if (this.readable) {
|
||||||
|
// readable and writable
|
||||||
|
this.writable = false;
|
||||||
|
|
||||||
if (this.secure) {
|
if (this.secure) {
|
||||||
this._shutdownSecure();
|
this._shutdownSecure();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
shutdown(this.fd, 'write')
|
||||||
|
} catch (e) {
|
||||||
|
this.destroy(e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// writable but not readable
|
||||||
|
this.destroy();
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
shutdown(this.fd, 'write')
|
|
||||||
} catch (e) {
|
|
||||||
this.destroy(e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!this.readable) this.destroy();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -132,8 +132,6 @@ process.__defineGetter__('stdout', function () {
|
|||||||
var net = module.requireNative('net');
|
var net = module.requireNative('net');
|
||||||
stdout = new net.Stream(process.binding('stdio').stdoutFD);
|
stdout = new net.Stream(process.binding('stdio').stdoutFD);
|
||||||
|
|
||||||
stdout.addListener('error', function (err) { throw err; });
|
|
||||||
|
|
||||||
return stdout;
|
return stdout;
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -143,7 +141,6 @@ process.openStdin = function () {
|
|||||||
var net = module.requireNative('net');
|
var net = module.requireNative('net');
|
||||||
var fd = process.binding('stdio').openStdin();
|
var fd = process.binding('stdio').openStdin();
|
||||||
stdin = new net.Stream(fd);
|
stdin = new net.Stream(fd);
|
||||||
stdin.addListener('error', function (err) { throw err; });
|
|
||||||
stdin.resume();
|
stdin.resume();
|
||||||
stdin.readable = true;
|
stdin.readable = true;
|
||||||
return stdin;
|
return stdin;
|
||||||
|
@ -5,9 +5,9 @@ exec = require("child_process").exec;
|
|||||||
|
|
||||||
body = "hello world\n";
|
body = "hello world\n";
|
||||||
server = http.createServer(function (req, res) {
|
server = http.createServer(function (req, res) {
|
||||||
res.writeHead(200, {
|
res.writeHead(200, {
|
||||||
"Content-Length": body.length,
|
"Content-Length": body.length,
|
||||||
"Content-Type": "text/plain",
|
"Content-Type": "text/plain"
|
||||||
});
|
});
|
||||||
res.write(body);
|
res.write(body);
|
||||||
res.end();
|
res.end();
|
||||||
|
@ -5,6 +5,8 @@ starttime = null;
|
|||||||
timeouttime = null;
|
timeouttime = null;
|
||||||
timeout = 1000;
|
timeout = 1000;
|
||||||
|
|
||||||
|
gotError = false
|
||||||
|
|
||||||
var echo_server = net.createServer(function (socket) {
|
var echo_server = net.createServer(function (socket) {
|
||||||
socket.setTimeout(timeout);
|
socket.setTimeout(timeout);
|
||||||
|
|
||||||
@ -14,6 +16,11 @@ var echo_server = net.createServer(function (socket) {
|
|||||||
p(timeouttime);
|
p(timeouttime);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
socket.addListener("error", function (e) {
|
||||||
|
assert.ok(e instanceof Error);
|
||||||
|
gotError = true;
|
||||||
|
})
|
||||||
|
|
||||||
socket.addListener("data", function (d) {
|
socket.addListener("data", function (d) {
|
||||||
puts(d);
|
puts(d);
|
||||||
socket.write(d);
|
socket.write(d);
|
||||||
@ -77,4 +84,6 @@ process.addListener("exit", function () {
|
|||||||
|
|
||||||
// Allow for 800 milliseconds more
|
// Allow for 800 milliseconds more
|
||||||
assert.ok(diff < timeout + 800);
|
assert.ok(diff < timeout + 800);
|
||||||
|
|
||||||
|
assert.ok(gotError);
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user