From b4bd0d437074a29a972ecdbdc3a026554657817e Mon Sep 17 00:00:00 2001 From: Mariusz Nowak Date: Thu, 20 Jan 2022 12:48:00 +0100 Subject: [PATCH] refactor(CLI): Seclude uncaught exception handling --- lib/cli/handle-error.js | 12 ++---------- scripts/serverless.js | 4 +++- test/unit/lib/cli/handle-error.test.js | 10 ---------- 3 files changed, 5 insertions(+), 21 deletions(-) diff --git a/lib/cli/handle-error.js b/lib/cli/handle-error.js index 10a4c6f4919..e6fd0fad166 100644 --- a/lib/cli/handle-error.js +++ b/lib/cli/handle-error.js @@ -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]/, ''); @@ -120,5 +113,4 @@ module.exports = async (exception, options = {}) => { } process.exitCode = 1; - if (isUncaughtException) process.exit(); }; diff --git a/scripts/serverless.js b/scripts/serverless.js index 4b2b6bab32c..a1c36d9cf81 100755 --- a/scripts/serverless.js +++ b/scripts/serverless.js @@ -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, @@ -52,6 +52,8 @@ process.once('uncaughtException', (error) => { hasTelemetryBeenReported: cachedHasTelemetryBeenReported, commandUsage, variableSourcesInConfig, + }).then(() => { + process.exit(); }); }); diff --git a/test/unit/lib/cli/handle-error.test.js b/test/unit/lib/cli/handle-error.test.js index 5bc91146181..a1658c720f5 100644 --- a/test/unit/lib/cli/handle-error.test.js +++ b/test/unit/lib/cli/handle-error.test.js @@ -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')));