diff --git a/packages/@aws-cdk/cdk/lib/core/construct.ts b/packages/@aws-cdk/cdk/lib/core/construct.ts index d6ca456427802..b6e1f680d83b1 100644 --- a/packages/@aws-cdk/cdk/lib/core/construct.ts +++ b/packages/@aws-cdk/cdk/lib/core/construct.ts @@ -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; diff --git a/packages/@aws-cdk/cdk/test/core/test.construct.ts b/packages/@aws-cdk/cdk/test/core/test.construct.ts index dc786b63b1288..4479c4709140d 100644 --- a/packages/@aws-cdk/cdk/test/core/test.construct.ts +++ b/packages/@aws-cdk/cdk/test/core/test.construct.ts @@ -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(); },