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 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 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
@@ -1,7 +1,10 @@
import { beforeEach, describe, it, mock } from 'node:test';
import { GenerateGraphqlClientCodeCommand } from './generate_graphql_client_code_command.js';
import yargs, { CommandModule } from 'yargs';
import { TestCommandRunner } from '../../../test-utils/command_runner.js';
import {
TestCommandError,
TestCommandRunner,
} from '../../../test-utils/command_runner.js';
import assert from 'node:assert';
import { BackendIdentifier } from '@aws-amplify/plugin-types';
import path from 'path';
Expand All @@ -18,6 +21,10 @@ import { BackendIdentifierResolverWithFallback } from '../../../backend-identifi
import { S3Client } from '@aws-sdk/client-s3';
import { AmplifyClient } from '@aws-sdk/client-amplify';
import { CloudFormationClient } from '@aws-sdk/client-cloudformation';
import {
BackendOutputClientError,
BackendOutputClientErrorType,
} from '@aws-amplify/deployed-backend-client';

void describe('generate graphql-client-code command', () => {
const generateApiCodeAdapter = new GenerateApiCodeAdapter({
Expand Down Expand Up @@ -355,4 +362,45 @@ void describe('generate graphql-client-code command', () => {
);
assert.match(output, /Arguments .* are mutually exclusive/);
});

void it('throws user error when NO_APP_FOUND_ERROR occurs', async () => {
mock.method(generateApiCodeAdapter, 'invokeGenerateApiCode', () => {
throw new BackendOutputClientError(
BackendOutputClientErrorType.NO_APP_FOUND_ERROR,
'No app found for stack stack_name'
);
});

await assert.rejects(
() =>
commandRunner.runCommand(
'graphql-client-code --app-id test-app --branch main'
),
(error: TestCommandError) => {
assert.strictEqual(error.error.name, 'AmplifyAppNotFoundError');
assert.strictEqual(
error.error.message,
'No app found for stack stack_name'
);
return true;
}
);
});

void it('re-throw other types of errors', async () => {
const originalError = new Error('Some other error');
mock.method(generateApiCodeAdapter, 'invokeGenerateApiCode', () => {
throw originalError;
});
await assert.rejects(
() =>
commandRunner.runCommand(
'graphql-client-code --app-id test-app --branch main'
),
(error: TestCommandError) => {
assert.strictEqual(error.error, originalError);
return true;
}
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
GenerateModelsOptions,
} from '@aws-amplify/model-generator';
import { ArgumentsKebabCase } from '../../../kebab_case.js';
import { AmplifyUserError } from '@aws-amplify/platform-core';
import {
BackendOutputClientError,
BackendOutputClientErrorType,
} from '@aws-amplify/deployed-backend-client';

type GenerateOptions =
| GenerateGraphqlCodegenOptions
Expand Down Expand Up @@ -89,20 +94,38 @@
handler = async (
args: ArgumentsCamelCase<GenerateGraphqlClientCodeCommandOptions>
): Promise<void> => {
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);
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);
const result = await this.generateApiCodeAdapter.invokeGenerateApiCode({
...backendIdentifier,
...formatParams,
} as unknown as InvokeGenerateApiCodeProps);

await result.writeToDirectory(out);
await result.writeToDirectory(out);
} catch (error) {
if (
error instanceof BackendOutputClientError &&

Check failure on line 114 in packages/cli/src/commands/generate/graphql-client-code/generate_graphql_client_code_command.ts

View workflow job for this annotation

GitHub Actions / lint

Do not use instanceof with BackendOutputClientError. Use BackendOutputClientError.isBackendOutputClientError instead
Copy link
Contributor

Choose a reason for hiding this comment

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

As per the lint, don't use instanceof as it might not work if there are multiple versions of DeployedBackendClient package in the node_modules.

error.code === BackendOutputClientErrorType.NO_APP_FOUND_ERROR
) {
throw new AmplifyUserError(
'AmplifyAppNotFoundError',
{
message: error.message,
resolution: `Ensure that an Amplify app exists in the region.`,
},
error
);
}
// Re-throw any other errors
throw error;
}
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
import { GenerateOutputsCommand } from './generate_outputs_command.js';
import { ClientConfigFormat } from '@aws-amplify/client-config';
import yargs, { CommandModule } from 'yargs';
import { TestCommandRunner } from '../../../test-utils/command_runner.js';
import {
TestCommandError,
TestCommandRunner,
} from '../../../test-utils/command_runner.js';
import assert from 'node:assert';
import { AppBackendIdentifierResolver } from '../../../backend-identifier/backend_identifier_resolver.js';
import { ClientConfigGeneratorAdapter } from '../../../client-config/client_config_generator_adapter.js';
Expand All @@ -11,6 +14,10 @@
import { S3Client } from '@aws-sdk/client-s3';
import { AmplifyClient } from '@aws-sdk/client-amplify';
import { CloudFormationClient } from '@aws-sdk/client-cloudformation';
import {
BackendOutputClientError,
BackendOutputClientErrorType,
} from '@aws-amplify/deployed-backend-client';

void describe('generate outputs command', () => {
const clientConfigGeneratorAdapter = new ClientConfigGeneratorAdapter({
Expand Down Expand Up @@ -248,4 +255,50 @@
);
assert.match(output, /Arguments .* mutually exclusive/);
});

void it('throws user error when NO_APP_FOUND_ERROR occurs', async () => {
// Mock the generator to throw NO_APP_FOUND_ERROR
mock.method(
clientConfigGeneratorAdapter,
'generateClientConfigToFile',
() => {
throw new BackendOutputClientError(
BackendOutputClientErrorType.NO_APP_FOUND_ERROR,
'No Amplify app found in the specified region'
);
}
);

await assert.rejects(
() => commandRunner.runCommand('outputs --app-id test-app --branch main'),
(error: TestCommandError) => {
assert.strictEqual(error.error.name, 'AmplifyAppNotFoundError');
assert.strictEqual(
error.error.message,
'No Amplify app found in the specified region'
);
return true;
}
);
});
void it('re-throw other types of errors'),
async () => {
Fixed Show fixed Hide fixed
const originalError = new Error('Some other error');
mock.method(
clientConfigGeneratorAdapter,
'generateClientConfigToFile',
() => {
throw originalError;
}
);

await assert.rejects(
() =>
commandRunner.runCommand('outputs --app-id test-app --branch main'),
(error: Error) => {
assert.strictEqual(error, originalError);
return true;
}
);
};
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import { ClientConfigGeneratorAdapter } from '../../../client-config/client_config_generator_adapter.js';
import { ArgumentsKebabCase } from '../../../kebab_case.js';
import { AmplifyUserError } from '@aws-amplify/platform-core';
import {
BackendOutputClientError,
BackendOutputClientErrorType,
} from '@aws-amplify/deployed-backend-client';

export type GenerateOutputsCommandOptions =
ArgumentsKebabCase<GenerateOutputsCommandOptionsCamelCase>;
Expand Down Expand Up @@ -55,25 +59,43 @@
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.',
});
}
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
);
await this.clientConfigGenerator.generateClientConfigToFile(
backendIdentifier,
args.outputsVersion as ClientConfigVersion,
args.outDir,
args.format
);
} catch (error) {
if (
error instanceof BackendOutputClientError &&

Check failure on line 84 in packages/cli/src/commands/generate/outputs/generate_outputs_command.ts

View workflow job for this annotation

GitHub Actions / lint

Do not use instanceof with BackendOutputClientError. Use BackendOutputClientError.isBackendOutputClientError instead
error.code === BackendOutputClientErrorType.NO_APP_FOUND_ERROR
) {
throw new AmplifyUserError(
'AmplifyAppNotFoundError',
{
message: error.message,
resolution: `Ensure that an Amplify app exists in the region.`,
},
error
);
}
// Re-throw any other errors
throw error;
}
};

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/deployed-backend-client/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ export enum BackendOutputClientErrorType {
// (undocumented)
METADATA_RETRIEVAL_ERROR = "MetadataRetrievalError",
// (undocumented)
NO_APP_FOUND_ERROR = "NoAppFoundError",
// (undocumented)
NO_OUTPUTS_FOUND = "NoOutputsFound",
// (undocumented)
NO_STACK_FOUND = "NoStackFound"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export enum BackendOutputClientErrorType {
NO_STACK_FOUND = 'NoStackFound',
CREDENTIALS_ERROR = 'CredentialsError',
ACCESS_DENIED = 'AccessDenied',
NO_APP_FOUND_ERROR = 'NoAppFoundError',
}
/**
* Error type for BackendOutputClientError
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { MainStackNameResolver } from '@aws-amplify/plugin-types';
import { AmplifyClient, ListAppsCommand } from '@aws-sdk/client-amplify';
import { BackendIdentifierConversions } from '@aws-amplify/platform-core';
import {
BackendOutputClientError,
BackendOutputClientErrorType,
} from '../backend_output_client_factory.js';
Comment on lines +4 to +7
Copy link
Contributor

@Amplifiyer Amplifiyer Dec 13, 2024

Choose a reason for hiding this comment

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

BackendOutputClientError is probably not the right error type to use here since based on it's name, it should be used with BackendOutputClient only.


/**
* Tuple of Amplify App name and branch
Expand Down Expand Up @@ -38,7 +42,8 @@ export class AppNameAndBranchMainStackNameResolver
);
const region = await this.amplifyClient.config.region();
if (appMatches.length === 0) {
throw new Error(
throw new BackendOutputClientError(
BackendOutputClientErrorType.NO_APP_FOUND_ERROR,
`No apps found with name ${this.appNameAndBranch.appName} in region ${region}`
);
} else if (appMatches.length >= 2) {
Expand Down
Loading