Skip to content

Commit 86d5254

Browse files
authored
fix(toolkit-lib): errors from async builders are not caught (#382)
Errors thrown from async functions passed to `fromAssemblyBuilder` would not be caught correctly: ```ts await toolkit.fromAssemblyBuilder(async () => { // Oops! Not wrapped the right type of error. throw new Error(...); }); ``` Due to a missing `await` in the `try/catch` handler. --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license
1 parent 4c3aaa8 commit 86d5254

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

packages/@aws-cdk/toolkit-lib/lib/api/cloud-assembly/private/source-builder.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ export abstract class CloudAssemblySourceBuilder {
5757
const env = await execution.defaultEnvVars();
5858
const assembly = await execution.changeDir(async () =>
5959
execution.withContext(context.all, env, props.synthOptions ?? {}, async (envWithContext, ctx) =>
60-
execution.withEnv(envWithContext, () => {
60+
execution.withEnv(envWithContext, async () => {
6161
try {
62-
return builder({
62+
return await builder({
6363
outdir: execution.outdir,
6464
context: ctx,
6565
});

packages/@aws-cdk/toolkit-lib/test/api/cloud-assembly/source-builder.test.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,17 @@ describe('fromAssemblyBuilder', () => {
3737
expect(JSON.stringify(stack)).toContain('amzn-s3-demo-bucket');
3838
});
3939

40-
test('errors are wrapped as AssemblyError', async () => {
40+
test.each(['sync', 'async'] as const)('errors are wrapped as AssemblyError for %s builder', async (sync) => {
4141
// GIVEN
42-
const cx = await toolkit.fromAssemblyBuilder(() => {
43-
throw new Error('a wild error appeared');
44-
});
42+
const builder = sync === 'sync'
43+
? () => {
44+
throw new Error('a wild error appeared');
45+
}
46+
: async () => {
47+
throw new Error('a wild error appeared');
48+
};
49+
50+
const cx = await toolkit.fromAssemblyBuilder(builder);
4551

4652
// WHEN
4753
try {
@@ -89,7 +95,7 @@ describe('fromAssemblyBuilder', () => {
8995
});
9096

9197
// WHEN
92-
await expect(cx.produce()).rejects.toThrow(/wild error/);
98+
await expect(cx.produce()).rejects.toThrow();
9399

94100
// THEN: Don't expect either a read or write lock on the directory afterwards
95101
expect(await (lock! as any)._currentWriter()).toBeUndefined();

0 commit comments

Comments
 (0)