More exit handling fixes

Summary:
Line 166 is missing a process.exit() causing the process to stay open forever if anything goes wrong.

Making everything use errorAndExit, and making use of the utility function so we don't need to remember when to print e, and when to print e.message.

Reviewed By: passy

Differential Revision: D15989164

fbshipit-source-id: c683fa07d98fe49283a59b5f8108ef54babc97a3
This commit is contained in:
John Knox
2019-06-26 09:14:33 -07:00
committed by Facebook Github Bot
parent 1fd5fa23a1
commit 34f835ace8
2 changed files with 38 additions and 27 deletions

View File

@@ -25,7 +25,9 @@ import {getActivePluginNames} from '../src/utils/pluginUtils.js';
import {serialize} from '../src/utils/serialization'; import {serialize} from '../src/utils/serialization';
import type BaseDevice from '../src/devices/BaseDevice'; import type BaseDevice from '../src/devices/BaseDevice';
type Action = {|exit: boolean, exitMessage?: string|}; import {getStringFromErrorLike} from '../src/utils/index';
type Action = {|exit: true, result: string|} | {|exit: false|};
type UserArguments = {| type UserArguments = {|
securePort: string, securePort: string,
@@ -112,8 +114,8 @@ function outputAndExit(output: string): void {
}); });
} }
function errorAndExit(error: string): void { function errorAndExit(error: any): void {
process.stderr.write(error, () => { process.stderr.write(getStringFromErrorLike(error), () => {
process.exit(1); process.exit(1);
}); });
} }
@@ -124,9 +126,13 @@ async function earlyExitActions(
originalConsole: typeof global.console, originalConsole: typeof global.console,
): Promise<void> { ): Promise<void> {
for (const exitAction of exitClosures) { for (const exitAction of exitClosures) {
const {exit, exitMessage} = await exitAction(userArguments); try {
if (exit) { const action = await exitAction(userArguments);
outputAndExit(exitMessage || 'Exited through the earlyExit action'); if (action.exit) {
outputAndExit(action.result);
}
} catch (e) {
errorAndExit(e);
} }
} }
} }
@@ -140,9 +146,13 @@ async function exitActions(
): Promise<void> { ): Promise<void> {
const {metrics, exit} = userArguments; const {metrics, exit} = userArguments;
for (var exitAction of exitClosures) { for (var exitAction of exitClosures) {
const {exit, exitMessage} = await exitAction(userArguments, store); try {
if (exit) { const action = await exitAction(userArguments, store);
outputAndExit(exitMessage || 'Exited through the exitActions function'); if (action.exit) {
outputAndExit(action.result);
}
} catch (e) {
errorAndExit(e);
} }
} }
@@ -162,7 +172,7 @@ async function exitActions(
outputAndExit(serializedString); outputAndExit(serializedString);
} }
} catch (e) { } catch (e) {
console.error(e); errorAndExit(e);
} }
}); });
} }
@@ -176,9 +186,13 @@ async function storeModifyingActions(
store: Store, store: Store,
): Promise<void> { ): Promise<void> {
for (const closure of storeModifyingClosures) { for (const closure of storeModifyingClosures) {
const {exit, exitMessage} = await closure(userArguments, store); try {
if (exit) { const action = await closure(userArguments, store);
outputAndExit(exitMessage || 'Exited through the earlyExit action'); if (action.exit) {
outputAndExit(action.result);
}
} catch (e) {
errorAndExit(e);
} }
} }
} }
@@ -229,7 +243,7 @@ async function startFlipper(userArguments: UserArguments) {
outputAndExit(payload || ''); outputAndExit(payload || '');
}) })
.catch(e => { .catch(e => {
errorAndExit(e.message); errorAndExit(e);
}); });
} else { } else {
exportStore(store) exportStore(store)
@@ -237,7 +251,7 @@ async function startFlipper(userArguments: UserArguments) {
outputAndExit(serializedString); outputAndExit(serializedString);
}) })
.catch(e => { .catch(e => {
errorAndExit(e.message); errorAndExit(e);
}); });
} }
}, 10); }, 10);
@@ -266,7 +280,7 @@ async function startFlipper(userArguments: UserArguments) {
serial: device.serial, serial: device.serial,
}; };
}); });
return {exit: true, exitMessage: serialize(mapped)}; return {exit: true, result: serialize(mapped)};
}); });
} }
return Promise.resolve({exit: false}); return Promise.resolve({exit: false});
@@ -303,10 +317,9 @@ async function startFlipper(userArguments: UserArguments) {
}); });
return {exit: false}; return {exit: false};
} }
return { return Promise.reject(
exit: true, new Error(`No device matching the serial ${selectedDeviceID}`),
exitMessage: `No device matching the serial ${selectedDeviceID}`, );
};
}); });
} }
return Promise.resolve({ return Promise.resolve({
@@ -323,9 +336,7 @@ async function startFlipper(userArguments: UserArguments) {
if (listPlugins) { if (listPlugins) {
return Promise.resolve({ return Promise.resolve({
exit: true, exit: true,
exitMessage: serialize( result: serialize(getActivePluginNames(store.getState().plugins)),
getActivePluginNames(store.getState().plugins),
),
}); });
} }
return Promise.resolve({ return Promise.resolve({
@@ -340,10 +351,10 @@ async function startFlipper(userArguments: UserArguments) {
pluginsClassMap(store.getState().plugins), pluginsClassMap(store.getState().plugins),
) )
.then(payload => { .then(payload => {
return {exit: true, exitMessage: payload ? payload.toString() : ''}; return {exit: true, result: payload ? payload.toString() : ''};
}) })
.catch(error => { .catch(error => {
return {exit: true, exitMessage: error}; return {exit: true, result: error};
}); });
} }
return Promise.resolve({exit: false}); return Promise.resolve({exit: false});

View File

@@ -290,11 +290,11 @@ export function exportStore(
const {exportData, errorArray} = await getStoreExport(store); const {exportData, errorArray} = await getStoreExport(store);
if (!exportData) { if (!exportData) {
console.error('Make sure a device is connected'); console.error('Make sure a device is connected');
reject('No device is selected'); reject(new Error('No device is selected'));
} }
const serializedString = serialize(exportData); const serializedString = serialize(exportData);
if (serializedString.length <= 0) { if (serializedString.length <= 0) {
reject('Serialize function returned empty string'); reject(new Error('Serialize function returned empty string'));
} }
resolve({serializedString, errorArray}); resolve({serializedString, errorArray});
} catch (e) { } catch (e) {