Skip to content

Commit

Permalink
wrap yargs validation errors in AmplifyUserError (#1739)
Browse files Browse the repository at this point in the history
* wrap yargs validation errors in AmplifyUserError

* PR Updates
  • Loading branch information
Amplifiyer authored Jul 15, 2024
1 parent 44ca7d7 commit ca92f23
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/lazy-teachers-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@aws-amplify/platform-core': patch
---

wrap yargs validation errors in AmplifyUserError
2 changes: 1 addition & 1 deletion packages/platform-core/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export abstract class AmplifyError<T extends string = string> extends Error {
// (undocumented)
readonly details?: string;
// (undocumented)
static fromError: (error: unknown) => AmplifyError<'UnknownFault' | 'CredentialsError'>;
static fromError: (error: unknown) => AmplifyError<'UnknownFault' | 'CredentialsError' | 'InvalidCommandInputError'>;
// (undocumented)
static fromStderr: (_stderr: string) => AmplifyError | undefined;
// (undocumented)
Expand Down
22 changes: 22 additions & 0 deletions packages/platform-core/src/errors/amplify_error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,25 @@ and some after the error message
assert.deepStrictEqual(actual?.resolution, 'test resolution');
});
});

void describe('AmplifyError.fromError', async () => {
void it('wraps Yargs validation errors in AmplifyUserError', () => {
const yargsErrors = [
new Error('Unknown command: asd'),
new Error('Unknown arguments: a,b,c'),
new Error('Missing required argument: d'),
new Error('Did you mean sandbox'),
new Error('Not enough non-option arguments: got 2, need at least 4'),
new Error('Invalid values: Arguments: a, Given: n, Choices: [b, c, d]'),
new Error('Arguments a and b are mutually exclusive'),
];
yargsErrors.forEach((error) => {
const actual = AmplifyError.fromError(error);
assert.ok(
actual instanceof AmplifyError &&
actual.name === 'InvalidCommandInputError',
`Failed the test for error ${error.message}`
);
});
});
});
44 changes: 39 additions & 5 deletions packages/platform-core/src/errors/amplify_error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,34 @@ export abstract class AmplifyError<T extends string = string> extends Error {

static fromError = (
error: unknown
): AmplifyError<'UnknownFault' | 'CredentialsError'> => {
): AmplifyError<
'UnknownFault' | 'CredentialsError' | 'InvalidCommandInputError'
> => {
const errorMessage =
error instanceof Error
? `${error.name}: ${error.message}`
: 'An unknown error happened. Check downstream error';

if (error instanceof Error && isCredentialsError(error)) {
return new AmplifyUserError('CredentialsError', {
message: errorMessage,
resolution: '',
});
return new AmplifyUserError(
'CredentialsError',
{
message: errorMessage,
resolution:
'Ensure your AWS credentials are correctly set and refreshed.',
},
error
);
}
if (error instanceof Error && isYargsValidationError(error)) {
return new AmplifyUserError(
'InvalidCommandInputError',
{
message: errorMessage,
resolution: 'Please see the underlying error message for resolution.',
},
error
);
}
return new AmplifyFault(
'UnknownFault',
Expand All @@ -126,6 +143,23 @@ const isCredentialsError = (err?: Error): boolean => {
return !!err && err?.name === 'CredentialsProviderError';
};

// These validation messages are taken from https://github.com/yargs/yargs/blob/0c95f9c79e1810cf9c8964fbf7d139009412f7e7/lib/validation.ts
const isYargsValidationError = (err?: Error): boolean => {
return (
!!err &&
([
'Unknown command',
'Unknown argument',
'Did you mean',
'Not enough non-option arguments',
'Too many non-option arguments',
'Missing required argument',
'Invalid values:',
].some((message) => err.message.startsWith(message)) ||
err.message.endsWith('are mutually exclusive'))
);
};

/**
* Amplify exception classifications
*/
Expand Down

0 comments on commit ca92f23

Please sign in to comment.