Skip to content

Commit 7aad5e8

Browse files
authored
update fallback for backend id resolvers if stack, app id, or branch are in args (#2034)
* update fallback for backend id resolvers if stack, app id, or branch are in args * pr feedback
1 parent 87dbf41 commit 7aad5e8

File tree

6 files changed

+59
-16
lines changed

6 files changed

+59
-16
lines changed

.changeset/itchy-vans-behave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@aws-amplify/backend-cli': patch
3+
---
4+
5+
update fallback for backend id resolvers if stack, app id, or branch are in args

packages/cli/src/backend-identifier/backend_identifier_with_sandbox_fallback.test.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void it('if backend identifier resolves without error, the resolved id is return
2626
});
2727
});
2828

29-
void it('uses the sandbox id if the default identifier resolver fails', async () => {
29+
void it('uses the sandbox id if the default identifier resolver fails and there is no stack, appId or branch in args', async () => {
3030
const appName = 'testAppName';
3131
const namespaceResolver = {
3232
resolve: () => Promise.resolve(appName),
@@ -54,3 +54,28 @@ void it('uses the sandbox id if the default identifier resolver fails', async ()
5454
name: 'test-user',
5555
});
5656
});
57+
58+
void it('does not use sandbox id if the default identifier resolver fails and there is stack, appId or branch in args', async () => {
59+
const appName = 'testAppName';
60+
const namespaceResolver = {
61+
resolve: () => Promise.resolve(appName),
62+
};
63+
64+
const defaultResolver = new AppBackendIdentifierResolver(namespaceResolver);
65+
const username = 'test-user';
66+
const sandboxResolver = new SandboxBackendIdResolver(
67+
namespaceResolver,
68+
() =>
69+
({
70+
username,
71+
} as never)
72+
);
73+
const backendIdResolver = new BackendIdentifierResolverWithFallback(
74+
defaultResolver,
75+
sandboxResolver
76+
);
77+
const resolvedId = await backendIdResolver.resolveDeployedBackendIdentifier({
78+
appId: 'testAppName',
79+
});
80+
assert.deepEqual(resolvedId, undefined);
81+
});

packages/cli/src/backend-identifier/backend_identifier_with_sandbox_fallback.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,25 @@ export class BackendIdentifierResolverWithFallback
1818
private fallbackResolver: SandboxBackendIdResolver
1919
) {}
2020
/**
21-
* resolves the backend id, falling back to the sandbox id if there is an error
21+
* resolves to deployed backend id, falling back to the sandbox id if stack or appId and branch inputs are not provided
2222
*/
2323
resolveDeployedBackendIdentifier = async (
2424
args: BackendIdentifierParameters
2525
) => {
26-
return (
27-
(await this.defaultResolver.resolveDeployedBackendIdentifier(args)) ??
28-
(await this.fallbackResolver.resolve())
29-
);
26+
if (args.stack || args.appId || args.branch) {
27+
return this.defaultResolver.resolveDeployedBackendIdentifier(args);
28+
}
29+
30+
return this.fallbackResolver.resolve();
3031
};
3132
/**
32-
* Resolves deployed backend id to backend id, falling back to the sandbox id if there is an error
33+
* Resolves deployed backend id to backend id, falling back to the sandbox id if stack or appId and branch inputs are not provided
3334
*/
3435
resolveBackendIdentifier = async (args: BackendIdentifierParameters) => {
35-
return (
36-
(await this.defaultResolver.resolveBackendIdentifier(args)) ??
37-
(await this.fallbackResolver.resolve())
38-
);
36+
if (args.stack || args.appId || args.branch) {
37+
return this.defaultResolver.resolveBackendIdentifier(args);
38+
}
39+
40+
return this.fallbackResolver.resolve();
3941
};
4042
}

packages/cli/src/commands/generate/forms/generate_forms_command.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ export class GenerateFormsCommand
6969
);
7070

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

7579
const backendOutputClient = this.backendOutputClientBuilder();

packages/cli/src/commands/generate/outputs/generate_outputs_command.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import { BackendIdentifierResolver } from '../../../backend-identifier/backend_identifier_resolver.js';
99
import { ClientConfigGeneratorAdapter } from '../../../client-config/client_config_generator_adapter.js';
1010
import { ArgumentsKebabCase } from '../../../kebab_case.js';
11+
import { AmplifyUserError } from '@aws-amplify/platform-core';
1112

1213
export type GenerateOutputsCommandOptions =
1314
ArgumentsKebabCase<GenerateOutputsCommandOptionsCamelCase>;
@@ -60,7 +61,11 @@ export class GenerateOutputsCommand
6061
);
6162

6263
if (!backendIdentifier) {
63-
throw new Error('Could not resolve the backend identifier');
64+
throw new AmplifyUserError('BackendIdentifierResolverError', {
65+
message: 'Could not resolve the backend identifier.',
66+
resolution:
67+
'Ensure stack name or Amplify App ID and branch specified are correct and exists, then re-run this command.',
68+
});
6469
}
6570

6671
await this.clientConfigGenerator.generateClientConfigToFile(

packages/cli/src/commands/generate/schema-from-database/generate_schema_command.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { BackendIdentifierResolver } from '../../../backend-identifier/backend_i
33
import { ArgumentsKebabCase } from '../../../kebab_case.js';
44
import { SecretClient } from '@aws-amplify/backend-secret';
55
import { SchemaGenerator } from '@aws-amplify/schema-generator';
6-
import { AmplifyFault } from '@aws-amplify/platform-core';
6+
import { AmplifyUserError } from '@aws-amplify/platform-core';
77

88
const DEFAULT_OUTPUT = 'amplify/data/schema.sql.ts';
99

@@ -57,8 +57,10 @@ export class GenerateSchemaCommand
5757
await this.backendIdentifierResolver.resolveBackendIdentifier(args);
5858

5959
if (!backendIdentifier) {
60-
throw new AmplifyFault('BackendIdentifierFault', {
61-
message: 'Could not resolve the backend identifier',
60+
throw new AmplifyUserError('BackendIdentifierResolverError', {
61+
message: 'Could not resolve the backend identifier.',
62+
resolution:
63+
'Ensure stack name or Amplify App ID and branch specified are correct and exists, then re-run this command.',
6264
});
6365
}
6466

0 commit comments

Comments
 (0)