Skip to content

Commit

Permalink
fix(core): improve error message if construct names conflict (#1706)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdnakt authored and rix0rrr committed Feb 8, 2019
1 parent 07d9909 commit 0ea4a78
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/@aws-cdk/cdk/lib/core/construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,8 @@ export class ConstructNode {
}

if (childName in this._children) {
throw new Error(`There is already a Construct with name '${childName}' in ${this.toString()}`);
const name = this.id || '';
throw new Error(`There is already a Construct with name '${childName}' in ${this.typename}${name.length > 0 ? ' [' + name + ']' : ''}`);
}

this._children[childName] = child;
Expand Down
11 changes: 10 additions & 1 deletion packages/@aws-cdk/cdk/test/core/test.construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,16 @@ export = {
// THEN: They have different paths
test.throws(() => {
new Construct(root, 'SameName');
});
}, /There is already a Construct with name 'SameName' in Root/);

// WHEN
const c0 = new Construct(root, 'c0');
new Construct(c0, 'SameName');

// THEN: They have different paths
test.throws(() => {
new Construct(c0, 'SameName');
}, /There is already a Construct with name 'SameName' in Construct \[c0\]/);

test.done();
},
Expand Down

0 comments on commit 0ea4a78

Please sign in to comment.