Skip to content

Commit

Permalink
throw user error when sandbox identifier input validation fails (#1979)
Browse files Browse the repository at this point in the history
* add changeset

* fix tests

* fix tests
  • Loading branch information
Amplifiyer authored Sep 10, 2024
1 parent 4ccd7bd commit ab09375
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/small-gorillas-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@aws-amplify/backend-cli': patch
---

throw user error when sandbox identifier validation fails
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions packages/cli/src/commands/sandbox/sandbox_command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { AmplifyError } from '@aws-amplify/platform-core';

mock.method(fsp, 'mkdir', () => Promise.resolve());

Expand Down Expand Up @@ -121,6 +122,24 @@ void describe('sandbox command', () => {
);
});

void it('throws AmplifyUserError if invalid identifier is provided', async () => {
const invalidIdentifier = 'invalid@';
await assert.rejects(
() =>
commandRunner.runCommand(`sandbox --identifier ${invalidIdentifier}`), // invalid identifier
(err: TestCommandError) => {
assert.ok(err.error instanceof AmplifyError);
assert.strictEqual(
err.error.message,
'Invalid --identifier provided: invalid@'
);
assert.strictEqual(err.error.name, 'InvalidCommandInputError');
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/);
Expand Down
9 changes: 6 additions & 3 deletions packages/cli/src/commands/sandbox/sandbox_command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<
{
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ index.ts(23,18): error TS2558: Expected 1 type arguments, but got 2.
index.ts(24,18): error TS2558: Expected 1 type arguments, but got 2.
index.ts(36,34): error TS2554: Expected 1 arguments, but got 2.
index.ts(42,34): error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'number | undefined'.
Type 'string' is not assignable to type 'number'.
index.ts(47,26): error TS2558: Expected 1 type arguments, but got 2.
index.ts(48,26): error TS2558: Expected 1 type arguments, but got 2.

0 comments on commit ab09375

Please sign in to comment.