Skip to content

Commit

Permalink
fix(lib): improve error message for passing an app to a construct
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMSchmidt committed Aug 10, 2023
1 parent 8a9a92c commit 8a3005c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
8 changes: 2 additions & 6 deletions packages/cdktf/lib/terraform-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,11 @@ export class TerraformStack extends Construct {

if (!node.scope) {
let hint = "";
if (
construct.node.scope === c &&
App.isApp(c) &&
TerraformBackend.isBackend(construct)
) {
if (construct.node.scope === c && App.isApp(c)) {
// the scope of the originally passed construct equals the construct c
// which has no scope (i.e. has no parent construct) and c is an App
// and our construct is a Backend
hint = `. You seem to have passed your root App as scope to a TerraformBackend construct. Pass a stack as scope to your backend instead.`;
hint = `. You seem to have passed your root App as scope to a construct. Pass a stack (inheriting from TerraformStack) as scope to your construct instead.`;
}

throw new Error(
Expand Down
4 changes: 2 additions & 2 deletions packages/cdktf/test/stack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ test("stack validation returns no error when provider is not set", () => {
expect(errors).toEqual([]);
});

test("getting Stack for TerraformBackend which was added to root app returns friendly error", () => {
test("getting Stack for construct which was added to root app returns friendly error", () => {
const app = Testing.stubVersion(new App({ stackTraces: false }));
new TerraformStack(app, "MyStack");

expect(() => new LocalBackend(app, {})).toThrowErrorMatchingInlineSnapshot(
`"No stack could be identified for the construct at path 'backend'. You seem to have passed your root App as scope to a TerraformBackend construct. Pass a stack as scope to your backend instead."`
`"No stack could be identified for the construct at path 'backend'. You seem to have passed your root App as scope to a construct. Pass a stack (inheriting from TerraformStack) as scope to your construct instead."`
);
});

Expand Down

0 comments on commit 8a3005c

Please sign in to comment.