From 3f61a4f846ad1b2afb8d938b2e4dd0a0ae76e8c6 Mon Sep 17 00:00:00 2001 From: Martin Bigio Date: Thu, 24 Sep 2015 05:07:18 -0700 Subject: [PATCH] Log server state when client is unexpetedly closed Reviewed By: @javache Differential Revision: D2473036 --- .../src/SocketInterface/SocketClient.js | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/react-packager/src/SocketInterface/SocketClient.js b/react-packager/src/SocketInterface/SocketClient.js index 0ff97442..0f86042a 100644 --- a/react-packager/src/SocketInterface/SocketClient.js +++ b/react-packager/src/SocketInterface/SocketClient.js @@ -31,6 +31,10 @@ class SocketClient { this._ready = new Promise((resolve, reject) => { this._sock.on('connect', () => { this._sock.removeAllListeners('error'); + process.on('uncaughtException', (error) => { + debug('uncaught error', error.stack); + setImmediate(() => process.exit(1)); + }); resolve(this); }); this._sock.on('error', (e) => { @@ -49,13 +53,24 @@ class SocketClient { this._sock.on('close', () => { if (!this._closing) { - const sockPathExists = fs.existsSync(sockPath); - throw new Error( - 'Server closed unexpectedly.\n' + - 'Socket path: `' + sockPath + '` ' + - (sockPathExists ? ' exists.' : 'doesn\'t exist') + '\n' + - getServerLogs() - ); + const terminate = (result) => { + const sockPathExists = fs.existsSync(sockPath); + throw new Error( + 'Server closed unexpectedly.\n' + + 'Server ping connection attempt result: ' + result + '\n' + + 'Socket path: `' + sockPath + '` ' + + (sockPathExists ? ' exists.' : 'doesn\'t exist') + '\n' + + getServerLogs() + ); + }; + + // before throwing ping the server to see if it's still alive + const socket = net.connect(sockPath); + socket.on('connect', () => { + socket.end(); + terminate('OK'); + }); + socket.on('error', error => terminate(error)); } }); }