Skip to content

Commit

Permalink
refactor(CLI): Seclude uncaught exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Jan 27, 2022
1 parent e597474 commit b4bd0d4
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 21 deletions.
12 changes: 2 additions & 10 deletions lib/cli/handle-error.js
Expand Up @@ -20,18 +20,11 @@ const isErrorCodeNormative = RegExp.prototype.test.bind(/^[A-Z][A-Z0-9]*(?:_[A-Z

module.exports = async (exception, options = {}) => {
if (!isObject(options)) options = {};

const {
isUncaughtException,
serverless,
hasTelemetryBeenReported,
commandUsage,
variableSourcesInConfig,
} = options;
const { serverless, hasTelemetryBeenReported, commandUsage, variableSourcesInConfig } = options;
const { command, options: cliOptions, commandSchema, serviceDir, configuration } = options;

const exceptionTokens = tokenizeException(exception);
const isUserError = !isUncaughtException && exceptionTokens.isUserError;
const isUserError = exceptionTokens.isUserError;

const platform = process.platform;
const nodeVersion = process.version.replace(/^[v|V]/, '');
Expand Down Expand Up @@ -120,5 +113,4 @@ module.exports = async (exception, options = {}) => {
}

process.exitCode = 1;
if (isUncaughtException) process.exit();
};
4 changes: 3 additions & 1 deletion scripts/serverless.js
Expand Up @@ -41,8 +41,8 @@ process.once('uncaughtException', (error) => {
progress.clear();
const cachedHasTelemetryBeenReported = hasTelemetryBeenReported;
hasTelemetryBeenReported = true;
log.error('Uncaught exception');
handleError(error, {
isUncaughtException: true,
command,
options,
commandSchema,
Expand All @@ -52,6 +52,8 @@ process.once('uncaughtException', (error) => {
hasTelemetryBeenReported: cachedHasTelemetryBeenReported,
commandUsage,
variableSourcesInConfig,
}).then(() => {
process.exit();
});
});

Expand Down
10 changes: 0 additions & 10 deletions test/unit/lib/cli/handle-error.test.js
Expand Up @@ -23,16 +23,6 @@ describe('test/unit/lib/cli/handle-error.test.js', () => {
expect(output).to.have.string('SDK');
});

it('should support `isUncaughtException` option', async () => {
const processExitStub = sinon.stub(process, 'exit').returns();
try {
await handleError(new ServerlessError('Test error'), { isUncaughtException: true });
expect(processExitStub.called).to.be.true;
} finally {
processExitStub.restore();
}
});

if (isStandaloneExecutable) {
it('should report standalone installation', async () => {
const output = await observeOutput(() => handleError(new ServerlessError('Test error')));
Expand Down

0 comments on commit b4bd0d4

Please sign in to comment.