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

Add error mapping for Amplify app not found in specified region #2313

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ninety-coins-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@aws-amplify/backend-cli': patch
---

Added error mapping for app name not available in the region
51 changes: 36 additions & 15 deletions packages/cli/src/backend-identifier/backend_identifier_resolver.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { DeployedBackendIdentifier } from '@aws-amplify/deployed-backend-client';
import { NamespaceResolver } from './local_namespace_resolver.js';
import { BackendIdentifier } from '@aws-amplify/plugin-types';
import { BackendIdentifierConversions } from '@aws-amplify/platform-core';
import {
AmplifyUserError,
BackendIdentifierConversions,
} from '@aws-amplify/platform-core';

export type BackendIdentifierParameters = {
stack?: string;
Expand Down Expand Up @@ -30,21 +33,39 @@ export class AppBackendIdentifierResolver implements BackendIdentifierResolver {
resolveDeployedBackendIdentifier = async (
args: BackendIdentifierParameters
): Promise<DeployedBackendIdentifier | undefined> => {
if (args.stack) {
return { stackName: args.stack };
} else if (args.appId && args.branch) {
return {
namespace: args.appId,
name: args.branch,
type: 'branch',
};
} else if (args.branch) {
return {
appName: await this.namespaceResolver.resolve(),
branchName: args.branch,
};
try {
if (args.stack) {
return { stackName: args.stack };
} else if (args.appId && args.branch) {
return {
namespace: args.appId,
name: args.branch,
type: 'branch',
};
} else if (args.branch) {
const resolvedNamespace = await this.namespaceResolver.resolve();
return {
appName: resolvedNamespace,
branchName: args.branch,
};
}
return undefined;
} catch (error) {
const appNotFoundMatch = (error as Error).message.match(
/No apps found with name (?<appName>.*) in region (?<region>.*)/
);

if (appNotFoundMatch?.groups) {
Copy link
Contributor

Choose a reason for hiding this comment

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

which line here is throwing the exception that you are catching? Afaik this.namespaceResolver.resolve(); doesn't throw this exception, but this does

`No apps found with name ${this.appNameAndBranch.appName} in region ${region}`
as I mentioned in my previous comment #2313 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The error originates in app_name_and_branch_main_stack_name_resolver.ts. Since we are unable to place AmplifyUserError in the deployed-backend-client, I've moved the error handling to
the CLI layer where the error bubbles up. I've successfully implemented error catching at this level. You can see in the screenshot I've added to the description.

Screenshot 2024-12-10 at 2 39 21 PM (2)

const { appName, region } = appNotFoundMatch.groups;
throw new AmplifyUserError('AmplifyAppNotFoundError', {
message: `No Amplify app found with name "${appName}" in region "${region}".`,
resolution: `Ensure that an Amplify app named "${appName}" exists in the "${region}" region.`,
});
}

// Re-throw any other errors
throw error;
}
return undefined;
};
resolveBackendIdentifier = async (
args: BackendIdentifierParameters
Expand Down
Loading