Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve error message on custom policies validation failure #9862

Merged
merged 1 commit into from Mar 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 8 additions & 9 deletions packages/amplify-cli-core/src/customPoliciesUtils.ts
Expand Up @@ -3,7 +3,7 @@ import { pathManager, stateManager } from './state-manager';
import Ajv from 'ajv';
import { EOL } from 'os';
import * as _ from 'lodash';
import { printer } from 'amplify-prompts';
import { formatter, printer } from 'amplify-prompts';
import { JSONUtilities } from './jsonUtilities';
import { CustomPoliciesFormatError } from './errors';

Expand Down Expand Up @@ -103,14 +103,13 @@ function validateCustomPolicies(data: CustomIAMPolicies, categoryName: string, r
const validatePolicy = ajv.compile(CustomIAMPoliciesSchema);
const valid = validatePolicy(data);
if (!valid) {
let errorMessage = `Invalid custom IAM policies in the ${resourceName} ${categoryName}.\n
Edit <project-dir>/amplify/backend/function/${resourceName}/custom-policies.json to fix
Learn more about custom IAM policies for ${categoryName}: https://docs.amplify.aws/cli/function/#access-existing-aws-resource-from-lambda-function\n`;
if (validatePolicy && validatePolicy.errors) {
errorMessage += validatePolicy.errors?.map((error: Ajv.ErrorObject) => error.message).join(EOL);
}

throw new CustomPoliciesFormatError(errorMessage);
printer.error(`${resourceName} ${categoryName} custom-policies.json failed validation:`);
formatter.list((validatePolicy?.errors || []).map(err => `${err.dataPath} ${err.message}`));
throw new CustomPoliciesFormatError(`
Invalid custom IAM policies for ${resourceName} ${categoryName}.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need an e2e test fix, since out messages have changed ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I searched for the original string in our test suite and didn't get any hits, so hopefully we're good

See details above and fix errors in <project-dir>/amplify/backend/${categoryName}/${resourceName}/custom-policies.json.
Learn more about custom IAM policies: https://docs.amplify.aws/cli/function/#access-existing-aws-resource-from-lambda-function
`);
}

for (const customPolicy of data) {
Expand Down