Skip to content

Commit

Permalink
wrap SyntaxErrors in AmplifyUserError (#1873)
Browse files Browse the repository at this point in the history
  • Loading branch information
rtpascual authored Aug 15, 2024
1 parent 8485526 commit 4cce19f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/friendly-melons-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@aws-amplify/platform-core': patch
---

wrap SyntaxErrors 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' | 'InvalidCommandInputError' | 'DomainNotFoundError'>;
static fromError: (error: unknown) => AmplifyError<'UnknownFault' | 'CredentialsError' | 'InvalidCommandInputError' | 'DomainNotFoundError' | 'SyntaxError'>;
// (undocumented)
static fromStderr: (_stderr: string) => AmplifyError | undefined;
// (undocumented)
Expand Down
9 changes: 9 additions & 0 deletions packages/platform-core/src/errors/amplify_error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,13 @@ void describe('AmplifyError.fromError', async () => {
`Failed the test for error ${error.message}`
);
});
void it('wraps SyntaxErrors in AmplifyUserError', () => {
const error = new Error('Typescript validation check failed.');
error.name = 'SyntaxError';
const actual = AmplifyError.fromError(error);
assert.ok(
actual instanceof AmplifyError && actual.name === 'SyntaxError',
`Failed the test for error ${error.message}`
);
});
});
20 changes: 20 additions & 0 deletions packages/platform-core/src/errors/amplify_error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export abstract class AmplifyError<T extends string = string> extends Error {
| 'CredentialsError'
| 'InvalidCommandInputError'
| 'DomainNotFoundError'
| 'SyntaxError'
> => {
const errorMessage =
error instanceof Error
Expand Down Expand Up @@ -143,6 +144,21 @@ export abstract class AmplifyError<T extends string = string> extends Error {
error
);
}
/**
* catches SyntaxErrors that were somehow not instances of AmplifyError
* this can be removed once we can properly identify where AmplifyError is being stripped off
*/
if (error instanceof Error && isSyntaxError(error)) {
return new AmplifyUserError(
'SyntaxError',
{
message: error.message,
resolution:
'Check your backend definition in the `amplify` folder for syntax and type errors.',
},
error
);
}
return new AmplifyFault(
'UnknownFault',
{
Expand Down Expand Up @@ -180,6 +196,10 @@ const isENotFoundError = (err?: Error): boolean => {
return !!err && err.message.startsWith('getaddrinfo ENOTFOUND');
};

const isSyntaxError = (err?: Error): boolean => {
return !!err && err.name === 'SyntaxError';
};

/**
* Amplify exception classifications
*/
Expand Down

0 comments on commit 4cce19f

Please sign in to comment.