From b9193448d33d054748e7a3acd68cd7d9d3d10faf Mon Sep 17 00:00:00 2001 From: kdnakt Date: Fri, 8 Feb 2019 01:35:54 +0900 Subject: [PATCH] fix(core): fix error message when construct name is the same with a sibling --- packages/@aws-cdk/cdk/lib/core/construct.ts | 3 ++- packages/@aws-cdk/cdk/test/core/test.construct.ts | 11 ++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) 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(); },