Client should throw when server unexpectedly closes the connection

Reviewed By: @natthu

Differential Revision: D2425357
This commit is contained in:
Amjad Masad 2015-09-09 16:30:22 -07:00 committed by facebook-github-bot-7
parent b8209572bb
commit 2c321df75c
3 changed files with 32 additions and 14 deletions

View File

@ -157,14 +157,16 @@ class Server {
}
buildBundle(options) {
const opts = bundleOpts(options);
return this._bundler.bundle(
opts.entryFile,
opts.runModule,
opts.sourceMapUrl,
opts.dev,
opts.platform
);
return Promise.resolve().then(() => {
const opts = bundleOpts(options);
return this._bundler.bundle(
opts.entryFile,
opts.runModule,
opts.sourceMapUrl,
opts.dev,
opts.platform
);
});
}
buildBundleFromUrl(reqUrl) {

View File

@ -33,10 +33,7 @@ class SocketClient {
this._sock.on('error', (e) => {
e.message = `Error connecting to server on ${sockPath} ` +
`with error: ${e.message}`;
if (fs.existsSync(LOG_PATH)) {
e.message += '\nServer logs:\n' + fs.readFileSync(LOG_PATH, 'utf8');
}
e.message += getServerLogs();
reject(e);
});
@ -45,8 +42,13 @@ class SocketClient {
this._resolvers = Object.create(null);
const bunser = new bser.BunserBuf();
this._sock.on('data', (buf) => bunser.append(buf));
bunser.on('value', (message) => this._handleMessage(message));
this._sock.on('close', () => {
if (!this._closing) {
throw new Error('Server closed unexpectedly' + getServerLogs());
}
});
}
onReady() {
@ -105,6 +107,7 @@ class SocketClient {
close() {
debug('closing connection');
this._closing = true;
this._sock.end();
}
}
@ -115,3 +118,11 @@ function uid(len) {
len = len || 7;
return Math.random().toString(35).substr(2, len);
}
function getServerLogs() {
if (fs.existsSync(LOG_PATH)) {
return '\nServer logs:\n' + fs.readFileSync(LOG_PATH, 'utf8');
}
return '';
}

View File

@ -59,7 +59,12 @@ class SocketServer {
const bunser = new bser.BunserBuf();
sock.on('data', (buf) => bunser.append(buf));
bunser.on('value', (m) => this._handleMessage(sock, m));
bunser.on('error', (e) => console.error(e));
bunser.on('error', (e) => {
e.message = 'Unhandled error from the bser buffer. ' +
'Either error on encoding or message handling: \n' +
e.message;
throw e;
});
}
_handleMessage(sock, m) {