Skip to content

Commit 0114549

Browse files
author
Kamil Sobol
authored
Handle invalid package.json error (#2254)
1 parent d584b96 commit 0114549

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

.changeset/warm-garlics-raise.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@aws-amplify/backend-deployer': patch
3+
---
4+
5+
Handle invalid package.json error

packages/backend-deployer/src/cdk_error_mapper.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,19 @@ const testErrorMappings = [
386386
errorName: 'GetLambdaLayerVersionError',
387387
expectedDownstreamErrorMessage: undefined,
388388
},
389+
{
390+
// eslint-disable-next-line spellcheck/spell-checker
391+
errorMessage: `Error: npm error code EJSONPARSE
392+
npm error path /home/some-path/package.json
393+
npm error JSON.parse Expected double-quoted property name in JSON at position 868 while parsing near ...sbuild\\: \\^0.20.2\\,\\n<<<<<<< HEAD\\n\\t\\t\\hl-j...
394+
npm error JSON.parse Failed to parse JSON data.
395+
npm error JSON.parse Note: package.json must be actual JSON, not just JavaScript.
396+
npm error A complete log of this run can be found in: /home/some-path/.npm/_logs/2024-10-01T19_56_46_705Z-debug-0.log`,
397+
expectedTopLevelErrorMessage:
398+
'The /home/some-path/package.json is not a valid JSON.',
399+
errorName: 'InvalidPackageJsonError',
400+
expectedDownstreamErrorMessage: undefined,
401+
},
389402
];
390403

391404
void describe('invokeCDKCommand', { concurrency: 1 }, () => {
@@ -401,8 +414,8 @@ void describe('invokeCDKCommand', { concurrency: 1 }, () => {
401414
const humanReadableError = cdkErrorMapper.getAmplifyError(
402415
new Error(errorMessage)
403416
);
404-
assert.equal(humanReadableError.message, expectedTopLevelErrorMessage);
405417
assert.equal(humanReadableError.name, expectedErrorName);
418+
assert.equal(humanReadableError.message, expectedTopLevelErrorMessage);
406419
expectedDownstreamErrorMessage &&
407420
assert.equal(
408421
humanReadableError.cause?.message,

packages/backend-deployer/src/cdk_error_mapper.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,15 @@ export class CdkErrorMapper {
278278
errorName: 'CFNUpdateNotSupportedError',
279279
classification: 'ERROR',
280280
},
281+
{
282+
errorRegex: new RegExp(
283+
`npm error code EJSONPARSE${this.multiLineEolRegex}npm error path (?<filePath>.*/package\\.json)${this.multiLineEolRegex}(npm error (.*)${this.multiLineEolRegex})*`
284+
),
285+
humanReadableErrorMessage: 'The {filePath} is not a valid JSON.',
286+
resolutionMessage: `Check package.json file and make sure it is a valid JSON.`,
287+
errorName: 'InvalidPackageJsonError',
288+
classification: 'ERROR',
289+
},
281290
{
282291
// Error: .* is printed to stderr during cdk synth
283292
// Also extracts the first line in the stack where the error happened
@@ -368,6 +377,7 @@ export type CDKDeploymentError =
368377
| 'ExpiredTokenError'
369378
| 'FileConventionError'
370379
| 'ModuleNotFoundError'
380+
| 'InvalidPackageJsonError'
371381
| 'SecretNotSetError'
372382
| 'SyntaxError'
373383
| 'GetLambdaLayerVersionError';

0 commit comments

Comments
 (0)