From 395bdc8b23b0fd5a94fee3584df89f30232e6d74 Mon Sep 17 00:00:00 2001 From: Piotr Grzesik Date: Thu, 22 Apr 2021 15:24:10 +0200 Subject: [PATCH] refactor: Add `code` to `ServerlessError` in `lib/configuration` --- lib/configuration/read.js | 2 +- lib/configuration/resolve-provider-name.js | 1 + lib/configuration/variables/sources/env.js | 1 + lib/configuration/variables/sources/file.js | 2 ++ .../sources/instance-dependent/get-cf.js | 1 + .../sources/instance-dependent/get-s3.js | 1 + .../sources/instance-dependent/get-sls.js | 1 + .../sources/instance-dependent/get-ssm.js | 1 + lib/configuration/variables/sources/opt.js | 1 + .../variables/sources/str-to-bool.js | 1 + .../configuration/resolve-provider-name.test.js | 17 +++++++++++------ 11 files changed, 22 insertions(+), 7 deletions(-) diff --git a/lib/configuration/read.js b/lib/configuration/read.js index aaaaa6322..89fe28dc6 100644 --- a/lib/configuration/read.js +++ b/lib/configuration/read.js @@ -37,7 +37,7 @@ const resolveTsNode = async (serviceDir) => { return require.resolve(`${String(stdoutBuffer).trim()}/ts-node`); } catch (globalDepError) { if (globalDepError.code !== 'MODULE_NOT_FOUND') throw globalDepError; - throw new ServerlessError('"ts-node" not found'); + throw new ServerlessError('"ts-node" not found', 'TS_NODE_NOT_FOUND'); } } } diff --git a/lib/configuration/resolve-provider-name.js b/lib/configuration/resolve-provider-name.js index d0ec19dd4..0a5d1022c 100644 --- a/lib/configuration/resolve-provider-name.js +++ b/lib/configuration/resolve-provider-name.js @@ -12,6 +12,7 @@ module.exports = (configuration) => { { Error: ServerlessError, errorMessage: 'Invalid service configuration: "provider.name" property is missing', + errorCode: 'INVALID_CONFIGURATION_PROVIDER_NAME_MISSING', } ); } catch (error) { diff --git a/lib/configuration/variables/sources/env.js b/lib/configuration/variables/sources/env.js index 9e535c410..9f1f15549 100644 --- a/lib/configuration/variables/sources/env.js +++ b/lib/configuration/variables/sources/env.js @@ -14,6 +14,7 @@ module.exports = { address = ensureString(address, { Error: ServerlessError, errorMessage: 'Non-string address argument in variable "env" source: %v', + errorCode: 'INVALID_ENV_SOURCE_ADDRESS_ARGUMENT', }); return { value: process.env[address] || null, isPending: !isSourceFulfilled }; diff --git a/lib/configuration/variables/sources/file.js b/lib/configuration/variables/sources/file.js index 9e2b217fe..4e6b697a9 100644 --- a/lib/configuration/variables/sources/file.js +++ b/lib/configuration/variables/sources/file.js @@ -33,6 +33,7 @@ module.exports = { ensureString(params[0], { Error: ServerlessError, errorMessage: 'Non-string path argument in variable "file" source: %v', + errorCode: 'INVALID_FILE_SOURCE_PATH_ARGUMENT', }) ); if (!filePath.startsWith(`${serviceDir}${path.sep}`)) { @@ -45,6 +46,7 @@ module.exports = { address = ensureString(address, { Error: ServerlessError, errorMessage: 'Non-string address argument for variable "file" source: %v', + errorCode: 'INVALID_FILE_SOURCE_ADDRESS_ARGUMENT', }); } diff --git a/lib/configuration/variables/sources/instance-dependent/get-cf.js b/lib/configuration/variables/sources/instance-dependent/get-cf.js index 2507012b8..382518b4a 100644 --- a/lib/configuration/variables/sources/instance-dependent/get-cf.js +++ b/lib/configuration/variables/sources/instance-dependent/get-cf.js @@ -17,6 +17,7 @@ module.exports = (serverlessInstance) => { address = ensureString(address, { Error: ServerlessError, errorMessage: 'Non-string address argument in variable "cf" source: %v', + errorCode: 'INVALID_CF_SOURCE_ADDRESS_ARGUMENT', }); const separatorIndex = address.indexOf('.'); if (separatorIndex === -1) { diff --git a/lib/configuration/variables/sources/instance-dependent/get-s3.js b/lib/configuration/variables/sources/instance-dependent/get-s3.js index 8da1a0338..e48facee7 100644 --- a/lib/configuration/variables/sources/instance-dependent/get-s3.js +++ b/lib/configuration/variables/sources/instance-dependent/get-s3.js @@ -16,6 +16,7 @@ module.exports = (serverlessInstance) => { address = ensureString(address, { Error: ServerlessError, errorMessage: 'Non-string address argument in variable "s3" source: %v', + errorCode: 'INVALID_S3_SOURCE_ADDRESS_ARGUMENT', }); const separatorIndex = address.indexOf('/'); if (separatorIndex === -1) { diff --git a/lib/configuration/variables/sources/instance-dependent/get-sls.js b/lib/configuration/variables/sources/instance-dependent/get-sls.js index 127ad6f4d..74fdceaea 100644 --- a/lib/configuration/variables/sources/instance-dependent/get-sls.js +++ b/lib/configuration/variables/sources/instance-dependent/get-sls.js @@ -15,6 +15,7 @@ module.exports = (serverlessInstance) => { address = ensureString(address, { Error: ServerlessError, errorMessage: 'Non-string address argument in variable "sls" source: %v', + errorCode: 'INVALID_SLS_SOURCE_ADDRESS_ARGUMENT', }); switch (address) { diff --git a/lib/configuration/variables/sources/instance-dependent/get-ssm.js b/lib/configuration/variables/sources/instance-dependent/get-ssm.js index 36f7b424e..bf2623542 100644 --- a/lib/configuration/variables/sources/instance-dependent/get-ssm.js +++ b/lib/configuration/variables/sources/instance-dependent/get-ssm.js @@ -19,6 +19,7 @@ module.exports = (serverlessInstance) => { address = ensureString(address, { Error: ServerlessError, errorMessage: 'Non-string address argument in variable "ssm" source: %v', + errorCode: 'INVALID_SSM_SOURCE_ADDRESS_ARGUMENT', }); const region = !params || !params[0] || params[0] === 'raw' ? undefined : params[0]; const shouldReturnRawValue = params && (params[0] === 'raw' || params[1] === 'raw'); diff --git a/lib/configuration/variables/sources/opt.js b/lib/configuration/variables/sources/opt.js index c6dd838f4..e543444f8 100644 --- a/lib/configuration/variables/sources/opt.js +++ b/lib/configuration/variables/sources/opt.js @@ -9,6 +9,7 @@ module.exports = { isOptional: true, Error: ServerlessError, errorMessage: 'Non-string address argument in variable "opt" source: %v', + errorCode: 'INVALID_OPT_SOURCE_ADDRESS_ARGUMENT', }); if (!isSourceFulfilled) { if (address == null) return { value: null, isPending: true }; diff --git a/lib/configuration/variables/sources/str-to-bool.js b/lib/configuration/variables/sources/str-to-bool.js index b9ab92b6c..9935ad785 100644 --- a/lib/configuration/variables/sources/str-to-bool.js +++ b/lib/configuration/variables/sources/str-to-bool.js @@ -15,6 +15,7 @@ module.exports = { const stringValue = ensureString(params[0], { Error: ServerlessError, errorMessage: 'Non-string "strToBool" input:. Received: %v', + errorCode: 'INVALID_STR_TO_BOOL_SOURCE_VALUE', }).trim(); if (trueStrings.has(stringValue)) return { value: true }; diff --git a/test/unit/lib/configuration/resolve-provider-name.test.js b/test/unit/lib/configuration/resolve-provider-name.test.js index ee24c19e8..10288b073 100644 --- a/test/unit/lib/configuration/resolve-provider-name.test.js +++ b/test/unit/lib/configuration/resolve-provider-name.test.js @@ -3,8 +3,9 @@ const { expect } = require('chai'); const resolveProviderName = require('../../../../lib/configuration/resolve-provider-name'); +const ServerlessError = require('../../../../lib/serverless-error'); -describe('test/unit/lib/configuration/resolve-provider.name.test.js', () => { +describe('test/unit/lib/configuration/resolve-provider-name.test.js', () => { it('should read name from "provider"', () => { expect(resolveProviderName({ provider: 'foo' })).to.equal('foo'); }); @@ -12,14 +13,18 @@ describe('test/unit/lib/configuration/resolve-provider.name.test.js', () => { expect(resolveProviderName({ provider: { name: 'foo' } })).to.equal('foo'); }); it('should reject missing "provider.name"', () => { - expect(() => resolveProviderName({ provider: {} })).to.throw('Invalid service configuration'); + expect(() => resolveProviderName({ provider: {} })) + .to.throw(ServerlessError) + .with.property('code', 'INVALID_CONFIGURATION_PROVIDER_NAME_MISSING'); }); it('should reject invalid "provider.name"', () => { - expect(() => resolveProviderName({ provider: { name: {} } })).to.throw( - 'Invalid service configuration' - ); + expect(() => resolveProviderName({ provider: { name: {} } })) + .to.throw(ServerlessError) + .with.property('code', 'INVALID_CONFIGURATION_PROVIDER_NAME_MISSING'); }); it('should reject missing "provider"', () => { - expect(() => resolveProviderName({})).to.throw('Invalid service configuration'); + expect(() => resolveProviderName({})) + .to.throw(ServerlessError) + .with.property('code', 'INVALID_CONFIGURATION_PROVIDER_NAME_MISSING'); }); });