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 9 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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
GenerateModelsOptions,
} from '@aws-amplify/model-generator';
import { ArgumentsKebabCase } from '../../../kebab_case.js';
import { AmplifyUserError } from '@aws-amplify/platform-core';

type GenerateOptions =
| GenerateGraphqlCodegenOptions
Expand Down Expand Up @@ -89,20 +90,37 @@ export class GenerateGraphqlClientCodeCommand
handler = async (
args: ArgumentsCamelCase<GenerateGraphqlClientCodeCommandOptions>
): Promise<void> => {
const backendIdentifier =
await this.backendIdentifierResolver.resolveDeployedBackendIdentifier(
args
try {
const backendIdentifier =
await this.backendIdentifierResolver.resolveDeployedBackendIdentifier(
args
);
const out = this.getOutDir(args);
const format = args.format ?? GenerateApiCodeFormat.GRAPHQL_CODEGEN;
const formatParams = this.formatParamBuilders[format](args);

const result = await this.generateApiCodeAdapter.invokeGenerateApiCode({
...backendIdentifier,
...formatParams,
} as unknown as InvokeGenerateApiCodeProps);

await result.writeToDirectory(out);
} catch (error) {
const appNotFoundMatch = (error as Error).message.match(
/No apps found with name (?<appName>.*) in region (?<region>.*)/
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of regex matching, we should update the error thrown to have contextual information. E.g. see this example here in the deployed-backend-client package

export enum BackendOutputClientErrorType {
METADATA_RETRIEVAL_ERROR = 'MetadataRetrievalError',
NO_OUTPUTS_FOUND = 'NoOutputsFound',
DEPLOYMENT_IN_PROGRESS = 'DeploymentInProgress',
NO_STACK_FOUND = 'NoStackFound',
CREDENTIALS_ERROR = 'CredentialsError',
ACCESS_DENIED = 'AccessDenied',
}
/**
* Error type for BackendOutputClientError
*/
export class BackendOutputClientError extends Error {
public code: BackendOutputClientErrorType;
and usage here 3cf0738#diff-6575515384ffad97860f57f5a6eea59fb54b2813c72d8c610b7c7a8c5cc29258

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

);
const out = this.getOutDir(args);
const format = args.format ?? GenerateApiCodeFormat.GRAPHQL_CODEGEN;
const formatParams = this.formatParamBuilders[format](args);

const result = await this.generateApiCodeAdapter.invokeGenerateApiCode({
...backendIdentifier,
...formatParams,
} as unknown as InvokeGenerateApiCodeProps);
if (appNotFoundMatch?.groups) {
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.`,
});
}

await result.writeToDirectory(out);
// Re-throw any other errors
throw error;
}
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,42 @@ export class GenerateOutputsCommand
handler = async (
args: ArgumentsCamelCase<GenerateOutputsCommandOptions>
): Promise<void> => {
const backendIdentifier =
await this.backendIdentifierResolver.resolveDeployedBackendIdentifier(
args
try {
const backendIdentifier =
await this.backendIdentifierResolver.resolveDeployedBackendIdentifier(
args
);

if (!backendIdentifier) {
throw new AmplifyUserError('BackendIdentifierResolverError', {
message: 'Could not resolve the backend identifier.',
resolution:
'Ensure stack name or Amplify App ID and branch specified are correct and exists, then re-run this command.',
});
}

await this.clientConfigGenerator.generateClientConfigToFile(
backendIdentifier,
args.outputsVersion as ClientConfigVersion,
args.outDir,
args.format
);
} catch (error) {
const appNotFoundMatch = (error as Error).message.match(
/No apps found with name (?<appName>.*) in region (?<region>.*)/
);

if (!backendIdentifier) {
throw new AmplifyUserError('BackendIdentifierResolverError', {
message: 'Could not resolve the backend identifier.',
resolution:
'Ensure stack name or Amplify App ID and branch specified are correct and exists, then re-run this command.',
});
}
if (appNotFoundMatch?.groups) {
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.`,
});
}

await this.clientConfigGenerator.generateClientConfigToFile(
backendIdentifier,
args.outputsVersion as ClientConfigVersion,
args.outDir,
args.format
);
// Re-throw any other errors
throw error;
}
};

/**
Expand Down
Loading