From 0ef891f75e878eb95e6e3f996552006f8eb2c4d0 Mon Sep 17 00:00:00 2001 From: Zachary Becknell Date: Wed, 22 Jul 2020 12:50:56 -0400 Subject: [PATCH] Blazor: Add element/component name for duplicate key (#24153) * Add element/component name for duplicate key * Add in keyword for frame param Co-authored-by: Javier Calvarro Nelson Co-authored-by: Javier Calvarro Nelson --- .../src/RenderTree/RenderTreeDiffBuilder.cs | 18 ++++++++++++++---- .../test/RenderTreeDiffBuilderTest.cs | 6 +++--- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/Components/Components/src/RenderTree/RenderTreeDiffBuilder.cs b/src/Components/Components/src/RenderTree/RenderTreeDiffBuilder.cs index a7892991f4..8f39258ddc 100644 --- a/src/Components/Components/src/RenderTree/RenderTreeDiffBuilder.cs +++ b/src/Components/Components/src/RenderTree/RenderTreeDiffBuilder.cs @@ -318,7 +318,7 @@ namespace Microsoft.AspNetCore.Components.RenderTree { if (result.ContainsKey(key)) { - ThrowExceptionForDuplicateKey(key); + ThrowExceptionForDuplicateKey(key, frame); } result[key] = new KeyedItemInfo(oldStartIndex, -1); @@ -341,7 +341,7 @@ namespace Microsoft.AspNetCore.Components.RenderTree { if (existingEntry.NewIndex >= 0) { - ThrowExceptionForDuplicateKey(key); + ThrowExceptionForDuplicateKey(key, frame); } result[key] = new KeyedItemInfo(existingEntry.OldIndex, newStartIndex); @@ -355,9 +355,19 @@ namespace Microsoft.AspNetCore.Components.RenderTree return result; } - private static void ThrowExceptionForDuplicateKey(object key) + private static void ThrowExceptionForDuplicateKey(object key, in RenderTreeFrame frame) { - throw new InvalidOperationException($"More than one sibling has the same key value, '{key}'. Key values must be unique."); + switch (frame.FrameType) + { + case RenderTreeFrameType.Component: + throw new InvalidOperationException($"More than one sibling of component '{frame.ComponentType}' has the same key value, '{key}'. Key values must be unique."); + + case RenderTreeFrameType.Element: + throw new InvalidOperationException($"More than one sibling of element '{frame.ElementName}' has the same key value, '{key}'. Key values must be unique."); + + default: + throw new InvalidOperationException($"More than one sibling has the same key value, '{key}'. Key values must be unique."); + } } private static object KeyValue(ref RenderTreeFrame frame) diff --git a/src/Components/Components/test/RenderTreeDiffBuilderTest.cs b/src/Components/Components/test/RenderTreeDiffBuilderTest.cs index 9f3ba71809..0ec9e95fa7 100644 --- a/src/Components/Components/test/RenderTreeDiffBuilderTest.cs +++ b/src/Components/Components/test/RenderTreeDiffBuilderTest.cs @@ -340,7 +340,7 @@ namespace Microsoft.AspNetCore.Components.Test // Act/Assert var ex = Assert.Throws(() => GetSingleUpdatedComponent()); - Assert.Equal("More than one sibling has the same key value, 'key1'. Key values must be unique.", ex.Message); + Assert.Equal("More than one sibling of element 'el' has the same key value, 'key1'. Key values must be unique.", ex.Message); } [Fact] @@ -357,7 +357,7 @@ namespace Microsoft.AspNetCore.Components.Test // Act/Assert var ex = Assert.Throws(() => GetSingleUpdatedComponent()); - Assert.Equal("More than one sibling has the same key value, 'key1'. Key values must be unique.", ex.Message); + Assert.Equal("More than one sibling of element 'el' has the same key value, 'key1'. Key values must be unique.", ex.Message); } [Fact] @@ -374,7 +374,7 @@ namespace Microsoft.AspNetCore.Components.Test // Act/Assert var ex = Assert.Throws(() => GetSingleUpdatedComponent()); - Assert.Equal("More than one sibling has the same key value, 'key1'. Key values must be unique.", ex.Message); + Assert.Equal("More than one sibling of element 'el' has the same key value, 'key1'. Key values must be unique.", ex.Message); } [Fact]