Skip to content

Blazor: Add element/component name for duplicate key #24153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/Components/Components/src/RenderTree/RenderTreeDiffBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ private static Dictionary<object, KeyedItemInfo> BuildKeyToInfoLookup(DiffContex
{
if (result.ContainsKey(key))
{
ThrowExceptionForDuplicateKey(key);
ThrowExceptionForDuplicateKey(key, frame);
}

result[key] = new KeyedItemInfo(oldStartIndex, -1);
Expand All @@ -341,7 +341,7 @@ private static Dictionary<object, KeyedItemInfo> BuildKeyToInfoLookup(DiffContex
{
if (existingEntry.NewIndex >= 0)
{
ThrowExceptionForDuplicateKey(key);
ThrowExceptionForDuplicateKey(key, frame);
}

result[key] = new KeyedItemInfo(existingEntry.OldIndex, newStartIndex);
Expand All @@ -355,9 +355,19 @@ private static Dictionary<object, KeyedItemInfo> BuildKeyToInfoLookup(DiffContex
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)
Expand Down
6 changes: 3 additions & 3 deletions src/Components/Components/test/RenderTreeDiffBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public void RejectsClashingKeysInOldTree()

// Act/Assert
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]
Expand All @@ -357,7 +357,7 @@ public void RejectsClashingKeysInNewTree()

// Act/Assert
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]
Expand All @@ -374,7 +374,7 @@ public void RejectsClashingKeysEvenIfAllPairsMatch()

// Act/Assert
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]
Expand Down