Skip to content

Commit 8688aa0

Browse files
Kamil Soboledwardfoyle
andauthored
classify package.json errors as user errors (#870)
* classify package.json errors as user errors * Update packages/platform-core/src/package_json_reader.ts Co-authored-by: Edward Foyle <[email protected]> * Update packages/cli/src/backend-identifier/local_namespace_resolver.ts Co-authored-by: Edward Foyle <[email protected]> --------- Co-authored-by: Edward Foyle <[email protected]>
1 parent f474ad4 commit 8688aa0

File tree

6 files changed

+26
-6
lines changed

6 files changed

+26
-6
lines changed

.changeset/brown-taxis-allow.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@aws-amplify/platform-core': patch
3+
'@aws-amplify/backend-cli': patch
4+
---
5+
6+
Classify package json parsing errors as user errors

packages/cli/src/backend-identifier/local_namespace_resolver.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { PackageJsonReader } from '@aws-amplify/platform-core';
1+
import {
2+
AmplifyUserError,
3+
PackageJsonReader,
4+
} from '@aws-amplify/platform-core';
25

36
export type NamespaceResolver = {
47
resolve: () => Promise<string>;
@@ -20,6 +23,10 @@ export class LocalNamespaceResolver implements NamespaceResolver {
2023
resolve = async () => {
2124
const name = this.packageJsonReader.readFromCwd().name;
2225
if (name) return name;
23-
throw new Error('Cannot load name from the package.json');
26+
throw new AmplifyUserError('InvalidPackageJsonError', {
27+
message: 'Cannot load name from the package.json',
28+
resolution:
29+
'Ensure you are running amplify commands in root of your project (i.e. in the parent of the `amplify` directory). Also ensure that your root package.json file has a "name" field.',
30+
});
2431
};
2532
}

packages/platform-core/API.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class AmplifyUserError extends AmplifyError {
6464
}
6565

6666
// @public
67-
export type AmplifyUserErrorType = 'InvalidSchemaAuthError' | 'InvalidSchemaError' | 'ExpiredTokenError' | 'CloudFormationDeploymentError' | 'CFNUpdateNotSupportedError' | 'SyntaxError' | 'BackendBuildError' | 'BootstrapNotDetectedError' | 'AccessDeniedError' | 'FileConventionError';
67+
export type AmplifyUserErrorType = 'InvalidPackageJsonError' | 'InvalidSchemaAuthError' | 'InvalidSchemaError' | 'ExpiredTokenError' | 'CloudFormationDeploymentError' | 'CFNUpdateNotSupportedError' | 'SyntaxError' | 'BackendBuildError' | 'BootstrapNotDetectedError' | 'AccessDeniedError' | 'FileConventionError';
6868

6969
// @public
7070
export class BackendIdentifierConversions {

packages/platform-core/src/errors/amplify_error.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ export type AmplifyErrorType = AmplifyUserErrorType | AmplifyLibraryFaultType;
116116
* Amplify error types
117117
*/
118118
export type AmplifyUserErrorType =
119+
| 'InvalidPackageJsonError'
119120
| 'InvalidSchemaAuthError'
120121
| 'InvalidSchemaError'
121122
| 'ExpiredTokenError'

packages/platform-core/src/package_json_reader.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void describe('Package JSON reader', () => {
5151
void it('throws when package json is not parse-able', async () => {
5252
fsReadFileSync.mock.mockImplementationOnce(() => 'not json content');
5353
assert.throws(() => packageJsonReader.read(testPath), {
54-
message: 'Could not JSON.parse the contents of /test_path',
54+
message: 'Could not parse the contents of /test_path',
5555
});
5656
});
5757
});

packages/platform-core/src/package_json_reader.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs from 'fs';
22
import path from 'path';
33
import z from 'zod';
4+
import { AmplifyUserError } from './errors';
45

56
/**
67
* return the package json
@@ -19,8 +20,13 @@ export class PackageJsonReader {
1920
fs.readFileSync(absolutePackageJsonPath, 'utf-8')
2021
);
2122
} catch (err) {
22-
throw new Error(
23-
`Could not JSON.parse the contents of ${absolutePackageJsonPath}`
23+
throw new AmplifyUserError(
24+
'InvalidPackageJsonError',
25+
{
26+
message: `Could not parse the contents of ${absolutePackageJsonPath}`,
27+
resolution: `Ensure that ${absolutePackageJsonPath} exists and is a valid JSON file`,
28+
},
29+
err as Error
2430
);
2531
}
2632
return packageJsonSchema.parse(jsonParsedValue);

0 commit comments

Comments
 (0)