Remove server error listener so that it throws

Summary: @​public
We swallow errors like it's nobody's business :(
Having an error handler that `reject`s after the promise has been resolved is a no-op. In node, if there is no `error` event handler then the error would throw.

So after we start listening and we want to resolve the promise, we remove the error listener so that we make sure errors actually throw.

Finally, I made the `uncaughtError` handler log `error.stack` so we can get an idea of what's going on.

Reviewed By: @martinbigio

Differential Revision: D2468472
This commit is contained in:
Amjad Masad 2015-09-22 15:45:03 -07:00 committed by facebook-github-bot-7
parent 0671b2b6f2
commit 0e8f6e4ed3
1 changed files with 10 additions and 3 deletions

View File

@ -23,8 +23,15 @@ class SocketServer {
this._server = net.createServer(); this._server = net.createServer();
this._server.listen(sockPath); this._server.listen(sockPath);
this._ready = new Promise((resolve, reject) => { this._ready = new Promise((resolve, reject) => {
this._server.on('error', (e) => reject(e)); this._server.once('error', (e) => reject(e));
this._server.on('listening', () => { this._server.once('listening', () => {
// Remove error listener so we make sure errors propagate.
this._server.removeAllListeners('error');
this._server.on(
'close',
() => debug('server closed')
);
debug( debug(
'Process %d listening on socket path %s ' + 'Process %d listening on socket path %s ' +
'for server with options %j', 'for server with options %j',
@ -41,7 +48,7 @@ class SocketServer {
}); });
process.on('uncaughtException', (error) => { process.on('uncaughtException', (error) => {
debug('uncaught error', error); debug('uncaught error', error.stack);
setImmediate(() => process.exit(1)); setImmediate(() => process.exit(1));
}); });