Skip to content

Commit

Permalink
fix: extract generic cdk asset publish failures (#2271)
Browse files Browse the repository at this point in the history
  • Loading branch information
Amplifiyer authored Nov 26, 2024
1 parent 273a9ba commit 0a360fb
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/poor-phones-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@aws-amplify/backend-deployer': patch
---

extract generic cdk asset publish failures
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/backend-deployer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"@aws-amplify/platform-core": "^1.2.2",
"@aws-amplify/plugin-types": "^1.4.0",
"execa": "^8.0.1",
"tsx": "^4.6.1"
"tsx": "^4.6.1",
"strip-ansi": "^6.0.1"
},
"peerDependencies": {
"aws-cdk": "^2.158.0",
Expand Down
18 changes: 18 additions & 0 deletions packages/backend-deployer/src/cdk_error_mapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,24 @@ npm error A complete log of this run can be found in: /home/some-path/.npm/_logs
errorName: 'AccessDeniedError',
expectedDownstreamErrorMessage: undefined,
},
{
// eslint-disable-next-line spellcheck/spell-checker
errorMessage: `[31mamplify-stack-sandbox-11[22m: fail: Bucket named 'cdk-abc-assets-11-us-west-2' exists, but we do not have access to it.
amplify-stack-sandbox-11: fail: Bucket named 'cdk-abc-assets-11-us-west-2' exists, but we do not have access to it.
Failed to publish asset abc:current_account-current_region`,
expectedTopLevelErrorMessage: `CDK failed to publish assets due to 'Bucket named 'cdk-abc-assets-11-us-west-2' exists, but we do not have access to it.'`,
errorName: 'CDKAssetPublishError',
expectedDownstreamErrorMessage: undefined,
},
{
// eslint-disable-next-line spellcheck/spell-checker
errorMessage: `[31mamplify-user-sandbox-c71414864a: fail: socket hang up
Failed to publish asset abc:current_account-current_region`,
expectedTopLevelErrorMessage: `CDK failed to publish assets due to 'socket hang up'`,
errorName: 'CDKAssetPublishError',
expectedDownstreamErrorMessage: undefined,
},
{
errorMessage:
`Error: Transform failed with 1 error:` +
Expand Down
17 changes: 15 additions & 2 deletions packages/backend-deployer/src/cdk_error_mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
AmplifyFault,
AmplifyUserError,
} from '@aws-amplify/platform-core';
import stripANSI from 'strip-ansi';
import { BackendDeployerOutputFormatter } from './types.js';

/**
Expand All @@ -27,13 +28,14 @@ export class CdkErrorMapper {
return amplifyError;
}

const errorMessage = stripANSI(error.message);
const matchingError = this.getKnownErrors().find((knownError) =>
knownError.errorRegex.test(error.message)
knownError.errorRegex.test(errorMessage)
);

if (matchingError) {
// Extract meaningful contextual information if available
const matchGroups = error.message.match(matchingError.errorRegex);
const matchGroups = errorMessage.match(matchingError.errorRegex);

if (matchGroups && matchGroups.length > 1) {
// If the contextual information can be used in the error message use it, else consider it as a downstream cause
Expand Down Expand Up @@ -400,6 +402,16 @@ export class CdkErrorMapper {
errorName: 'AppSyncResolverSyntaxError',
classification: 'ERROR',
},
{
errorRegex: new RegExp(
`amplify-.*-(branch|sandbox)-.*fail: (?<publishFailure>.*)${this.multiLineEolRegex}.*Failed to publish asset`,
'm'
),
humanReadableErrorMessage: `CDK failed to publish assets due to '{publishFailure}'`,
resolutionMessage: `Check the error message for more details.`,
errorName: 'CDKAssetPublishError',
classification: 'ERROR',
},
// Generic error printed by CDK. Order matters so keep this towards the bottom of this list
{
// Error: .* is printed to stderr during cdk synth
Expand Down Expand Up @@ -447,6 +459,7 @@ export type CDKDeploymentError =
| 'BootstrapNotDetectedError'
| 'BootstrapDetectionError'
| 'BootstrapOutdatedError'
| 'CDKAssetPublishError'
| 'CDKResolveAWSAccountError'
| 'CDKVersionMismatchError'
| 'CFNUpdateNotSupportedError'
Expand Down

0 comments on commit 0a360fb

Please sign in to comment.