From d75a791e7ae47b14153560ca8047090d9d6ad8c9 Mon Sep 17 00:00:00 2001 From: Praveen Gupta Date: Tue, 10 Sep 2024 15:41:58 +0200 Subject: [PATCH] add changeset --- .changeset/small-gorillas-remain.md | 5 +++++ package-lock.json | 8 +++---- .../commands/sandbox/sandbox_command.test.ts | 22 +++++++++++++++++++ .../src/commands/sandbox/sandbox_command.ts | 9 +++++--- 4 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 .changeset/small-gorillas-remain.md diff --git a/.changeset/small-gorillas-remain.md b/.changeset/small-gorillas-remain.md new file mode 100644 index 0000000000..03d12139de --- /dev/null +++ b/.changeset/small-gorillas-remain.md @@ -0,0 +1,5 @@ +--- +'@aws-amplify/backend-cli': patch +--- + +throw user error when sandbox identifier validation fails diff --git a/package-lock.json b/package-lock.json index d1f0b2600b..06b7cca03b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -24670,7 +24670,7 @@ }, "packages/ai-constructs": { "name": "@aws-amplify/ai-constructs", - "version": "0.1.1", + "version": "0.1.2", "license": "Apache-2.0", "dependencies": { "@aws-amplify/plugin-types": "^1.0.1", @@ -24869,7 +24869,7 @@ }, "packages/backend-secret": { "name": "@aws-amplify/backend-secret", - "version": "1.1.0", + "version": "1.1.1", "license": "Apache-2.0", "dependencies": { "@aws-amplify/platform-core": "^1.0.5", @@ -25511,11 +25511,11 @@ }, "packages/sandbox": { "name": "@aws-amplify/sandbox", - "version": "1.2.0", + "version": "1.2.1", "license": "Apache-2.0", "dependencies": { "@aws-amplify/backend-deployer": "^1.1.0", - "@aws-amplify/backend-secret": "^1.1.0", + "@aws-amplify/backend-secret": "^1.1.1", "@aws-amplify/cli-core": "^1.1.2", "@aws-amplify/client-config": "^1.1.3", "@aws-amplify/deployed-backend-client": "^1.3.0", diff --git a/packages/cli/src/commands/sandbox/sandbox_command.test.ts b/packages/cli/src/commands/sandbox/sandbox_command.test.ts index a240f33d59..5009343d10 100644 --- a/packages/cli/src/commands/sandbox/sandbox_command.test.ts +++ b/packages/cli/src/commands/sandbox/sandbox_command.test.ts @@ -21,6 +21,7 @@ import { createSandboxSecretCommand } from './sandbox-secret/sandbox_secret_comm import { ClientConfigGeneratorAdapter } from '../../client-config/client_config_generator_adapter.js'; import { CommandMiddleware } from '../../command_middleware.js'; import { PackageManagerController } from '@aws-amplify/plugin-types'; +import { AmplifyUserError } from '@aws-amplify/platform-core'; mock.method(fsp, 'mkdir', () => Promise.resolve()); @@ -121,6 +122,27 @@ void describe('sandbox command', () => { ); }); + void it('throws AmplifyUserError if invalid identifier is provided', async () => { + const invalidIdentifier = 'invalid@'; + const invalidIdentifierError = new AmplifyUserError( + 'InvalidCommandInputError', + { + message: `Invalid --identifier provided: ${invalidIdentifier}`, + resolution: + 'Use an identifier that matches [a-zA-Z0-9-] and is less than 15 characters.', + } + ); + await assert.rejects( + () => + commandRunner.runCommand(`sandbox --identifier ${invalidIdentifier}`), // invalid identifier + (err: TestCommandError) => { + assert.deepStrictEqual(err.error, invalidIdentifierError); + return true; + } + ); + assert.equal(sandboxStartMock.mock.callCount(), 0); + }); + void it('shows available options in help output', async () => { const output = await commandRunner.runCommand('sandbox --help'); assert.match(output, /--identifier/); diff --git a/packages/cli/src/commands/sandbox/sandbox_command.ts b/packages/cli/src/commands/sandbox/sandbox_command.ts index 1da7519f6a..356d0a8eb6 100644 --- a/packages/cli/src/commands/sandbox/sandbox_command.ts +++ b/packages/cli/src/commands/sandbox/sandbox_command.ts @@ -21,6 +21,7 @@ import { CommandMiddleware } from '../../command_middleware.js'; import { SandboxCommandGlobalOptions } from './option_types.js'; import { ArgumentsKebabCase } from '../../kebab_case.js'; import { PackageManagerController } from '@aws-amplify/plugin-types'; +import { AmplifyUserError } from '@aws-amplify/platform-core'; export type SandboxCommandOptionsKebabCase = ArgumentsKebabCase< { @@ -254,9 +255,11 @@ export class SandboxCommand if (argv.identifier) { const identifierRegex = /^[a-zA-Z0-9-]{1,15}$/; if (!argv.identifier.match(identifierRegex)) { - throw new Error( - `--identifier should match [a-zA-Z0-9-] and be less than 15 characters.` - ); + throw new AmplifyUserError('InvalidCommandInputError', { + message: `Invalid --identifier provided: ${argv.identifier}`, + resolution: + 'Use an identifier that matches [a-zA-Z0-9-] and is less than 15 characters.', + }); } } return true;