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 <jacalvar@microsoft.com>

Co-authored-by: Javier Calvarro Nelson <jacalvar@microsoft.com>
This commit is contained in:
Zachary Becknell 2020-07-22 12:50:56 -04:00 committed by GitHub
parent 113805aba8
commit 0ef891f75e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 7 deletions

View File

@ -318,7 +318,7 @@ namespace Microsoft.AspNetCore.Components.RenderTree
{ {
if (result.ContainsKey(key)) if (result.ContainsKey(key))
{ {
ThrowExceptionForDuplicateKey(key); ThrowExceptionForDuplicateKey(key, frame);
} }
result[key] = new KeyedItemInfo(oldStartIndex, -1); result[key] = new KeyedItemInfo(oldStartIndex, -1);
@ -341,7 +341,7 @@ namespace Microsoft.AspNetCore.Components.RenderTree
{ {
if (existingEntry.NewIndex >= 0) if (existingEntry.NewIndex >= 0)
{ {
ThrowExceptionForDuplicateKey(key); ThrowExceptionForDuplicateKey(key, frame);
} }
result[key] = new KeyedItemInfo(existingEntry.OldIndex, newStartIndex); result[key] = new KeyedItemInfo(existingEntry.OldIndex, newStartIndex);
@ -355,9 +355,19 @@ namespace Microsoft.AspNetCore.Components.RenderTree
return result; 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) private static object KeyValue(ref RenderTreeFrame frame)

View File

@ -340,7 +340,7 @@ namespace Microsoft.AspNetCore.Components.Test
// Act/Assert // Act/Assert
var ex = Assert.Throws<InvalidOperationException>(() => GetSingleUpdatedComponent()); var ex = Assert.Throws<InvalidOperationException>(() => 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] [Fact]
@ -357,7 +357,7 @@ namespace Microsoft.AspNetCore.Components.Test
// Act/Assert // Act/Assert
var ex = Assert.Throws<InvalidOperationException>(() => GetSingleUpdatedComponent()); var ex = Assert.Throws<InvalidOperationException>(() => 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] [Fact]
@ -374,7 +374,7 @@ namespace Microsoft.AspNetCore.Components.Test
// Act/Assert // Act/Assert
var ex = Assert.Throws<InvalidOperationException>(() => GetSingleUpdatedComponent()); var ex = Assert.Throws<InvalidOperationException>(() => 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] [Fact]