Skip to content

Commit 0a85696

Browse files
ShadowCat567Vieltojarvi
andauthored
Lint rule for cause propagation when wrapping errors (#2426)
* propagate error cause lint rule * turned on propagate error cause rule * added changeset * update cause propagation rule * update test * fixed test * fix package-lock * fix package-lock * added package lint rule should be applied to * updated faults and userErrors * update test * more test updates * update test * updated changeset, lint rule applies to conditionals * removed empty changeset, not needed * turns out the empty changeset was needed... * hopefully fixed the tests * Variable declarations are no longer a problem * moved expect ignores * fixed the test case, fixed violators * updated changesets * fixed some violations * removed ts-expect-error for type casting * fixed create-amplify violation * split traversal from reporting * changed name of function * moved if statement * move cause name collection --------- Co-authored-by: Vieltojarvi <[email protected]>
1 parent 040272d commit 0a85696

File tree

17 files changed

+392
-19
lines changed

17 files changed

+392
-19
lines changed

.changeset/bright-llamas-brush.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'@aws-amplify/integration-tests': patch
3+
'@aws-amplify/backend-deployer': patch
4+
'@aws-amplify/backend-function': patch
5+
'@aws-amplify/schema-generator': patch
6+
'@aws-amplify/cli-core': patch
7+
'@aws-amplify/backend': patch
8+
'@aws-amplify/backend-cli': patch
9+
'create-amplify': patch
10+
---
11+
12+
fixed violations to cause propagation lint rule

.changeset/quick-houses-carry.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

packages/backend-deployer/src/cdk_deployer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ export class CDKDeployer implements BackendDeployer {
190190
try {
191191
await childProcess;
192192
return cdkOutput;
193+
// eslint-disable-next-line amplify-backend-rules/propagate-error-cause
193194
} catch (error) {
194195
// swallow execa error if the cdk cli ran and produced some stderr.
195196
// Most of the time this error is noise(basically child exited with exit code...)

packages/backend-function/src/runtime/get_amplify_clients_configuration.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,13 @@ export const getAmplifyDataClientConfig = async (
171171
} catch (caught) {
172172
if (caught instanceof NoSuchKey) {
173173
throw new Error(
174-
'Error retrieving the schema from S3. Please confirm that your project has a `defineData` included in the `defineBackend` definition.'
174+
'Error retrieving the schema from S3. Please confirm that your project has a `defineData` included in the `defineBackend` definition.',
175+
{ cause: caught }
175176
);
176177
} else if (caught instanceof S3ServiceException) {
177178
throw new Error(
178-
`Error retrieving the schema from S3. You may need to grant this function authorization on the schema. ${caught.name}: ${caught.message}.`
179+
`Error retrieving the schema from S3. You may need to grant this function authorization on the schema. ${caught.name}: ${caught.message}.`,
180+
{ cause: caught }
179181
);
180182
} else {
181183
throw caught;

packages/backend/src/engine/backend-secret/lambda/backend_secret_fetcher.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ export const handleCreateUpdateEvent = async (
7070
throw new Error(
7171
`Failed to retrieve backend secret '${secretName}' for '${
7272
props.namespace
73-
}/${props.name}'. Reason: ${JSON.stringify(err)}`
73+
}/${props.name}'. Reason: ${JSON.stringify(err)}`,
74+
{ cause: secretErr }
7475
);
7576
}
7677
}
@@ -86,7 +87,8 @@ export const handleCreateUpdateEvent = async (
8687
throw new Error(
8788
`Failed to retrieve backend secret '${secretName}' for '${
8889
props.namespace
89-
}'. Reason: ${JSON.stringify(err)}`
90+
}'. Reason: ${JSON.stringify(err)}`,
91+
{ cause: err }
9092
);
9193
}
9294
}

packages/cli-core/src/package-manager-controller/execute_with_debugger_logger.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ export const executeWithDebugLogger = (
2828
);
2929

3030
return childProcess;
31-
} catch {
31+
} catch (err) {
3232
throw new Error(
3333
`\`${executable}${
3434
args ? ' ' + args.join(' ') : ''
35-
}\` did not exit successfully. Rerun with --debug for more information.`
35+
}\` did not exit successfully. Rerun with --debug for more information.`,
36+
{ cause: err }
3637
);
3738
}
3839
};

packages/cli-core/src/package-manager-controller/package_manager_controller_base.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,10 @@ export abstract class PackageManagerControllerBase
8080
this.initDefault,
8181
this.execa
8282
);
83-
} catch {
83+
} catch (err) {
8484
throw new Error(
85-
`\`${this.executable} init\` did not exit successfully. Initialize a valid JavaScript package before continuing.`
85+
`\`${this.executable} init\` did not exit successfully. Initialize a valid JavaScript package before continuing.`,
86+
{ cause: err }
8687
);
8788
}
8889

packages/cli/src/commands/sandbox/sandbox_command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ export class SandboxCommand
286286
try {
287287
stats = await fsp.stat(dir, {});
288288
} catch (e) {
289-
throw new Error(`--${option} ${dir} does not exist`);
289+
throw new Error(`--${option} ${dir} does not exist`, { cause: e });
290290
}
291291
if (!stats.isDirectory()) {
292292
throw new Error(`--${option} ${dir} is not a valid directory`);

packages/create-amplify/src/get_project_root.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,12 @@ void describe('getProjectRoot', () => {
8787
Promise.reject(expectedError)
8888
);
8989

90-
await assert.rejects(getProjectRoot, expectedError);
90+
await assert.rejects(getProjectRoot, (error: AmplifyUserError) => {
91+
assert.strictEqual(error.name, expectedError.name);
92+
assert.strictEqual(error.message, expectedError.message);
93+
assert.strictEqual(error.resolution, expectedError.resolution);
94+
return true;
95+
});
9196
assert.equal(fsMkDirSyncMock.mock.callCount(), 1);
9297
assert.equal(
9398
fsMkDirSyncMock.mock.calls[0].arguments[0],

packages/create-amplify/src/get_project_root.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,14 @@ export const getProjectRoot = async () => {
4040
try {
4141
await fsp.mkdir(projectRoot, { recursive: true });
4242
} catch (err) {
43-
throw new AmplifyUserError('ProjectDirectoryCreateError', {
44-
message: `Failed to create project directory`,
45-
resolution: `Ensure that ${projectRoot} is the correct path and you have write permissions to this location.`,
46-
});
43+
throw new AmplifyUserError(
44+
'ProjectDirectoryCreateError',
45+
{
46+
message: `Failed to create project directory`,
47+
resolution: `Ensure that ${projectRoot} is the correct path and you have write permissions to this location.`,
48+
},
49+
err as Error
50+
);
4751
}
4852
}
4953
return projectRoot;

0 commit comments

Comments
 (0)