Skip to content

Commit 0ef891f

Browse files
zbecknelljaviercn
andauthored
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 <[email protected]> Co-authored-by: Javier Calvarro Nelson <[email protected]>
1 parent 113805a commit 0ef891f

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/Components/Components/src/RenderTree/RenderTreeDiffBuilder.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ private static Dictionary<object, KeyedItemInfo> BuildKeyToInfoLookup(DiffContex
318318
{
319319
if (result.ContainsKey(key))
320320
{
321-
ThrowExceptionForDuplicateKey(key);
321+
ThrowExceptionForDuplicateKey(key, frame);
322322
}
323323

324324
result[key] = new KeyedItemInfo(oldStartIndex, -1);
@@ -341,7 +341,7 @@ private static Dictionary<object, KeyedItemInfo> BuildKeyToInfoLookup(DiffContex
341341
{
342342
if (existingEntry.NewIndex >= 0)
343343
{
344-
ThrowExceptionForDuplicateKey(key);
344+
ThrowExceptionForDuplicateKey(key, frame);
345345
}
346346

347347
result[key] = new KeyedItemInfo(existingEntry.OldIndex, newStartIndex);
@@ -355,9 +355,19 @@ private static Dictionary<object, KeyedItemInfo> BuildKeyToInfoLookup(DiffContex
355355
return result;
356356
}
357357

358-
private static void ThrowExceptionForDuplicateKey(object key)
358+
private static void ThrowExceptionForDuplicateKey(object key, in RenderTreeFrame frame)
359359
{
360-
throw new InvalidOperationException($"More than one sibling has the same key value, '{key}'. Key values must be unique.");
360+
switch (frame.FrameType)
361+
{
362+
case RenderTreeFrameType.Component:
363+
throw new InvalidOperationException($"More than one sibling of component '{frame.ComponentType}' has the same key value, '{key}'. Key values must be unique.");
364+
365+
case RenderTreeFrameType.Element:
366+
throw new InvalidOperationException($"More than one sibling of element '{frame.ElementName}' has the same key value, '{key}'. Key values must be unique.");
367+
368+
default:
369+
throw new InvalidOperationException($"More than one sibling has the same key value, '{key}'. Key values must be unique.");
370+
}
361371
}
362372

363373
private static object KeyValue(ref RenderTreeFrame frame)

src/Components/Components/test/RenderTreeDiffBuilderTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ public void RejectsClashingKeysInOldTree()
340340

341341
// Act/Assert
342342
var ex = Assert.Throws<InvalidOperationException>(() => GetSingleUpdatedComponent());
343-
Assert.Equal("More than one sibling has the same key value, 'key1'. Key values must be unique.", ex.Message);
343+
Assert.Equal("More than one sibling of element 'el' has the same key value, 'key1'. Key values must be unique.", ex.Message);
344344
}
345345

346346
[Fact]
@@ -357,7 +357,7 @@ public void RejectsClashingKeysInNewTree()
357357

358358
// Act/Assert
359359
var ex = Assert.Throws<InvalidOperationException>(() => GetSingleUpdatedComponent());
360-
Assert.Equal("More than one sibling has the same key value, 'key1'. Key values must be unique.", ex.Message);
360+
Assert.Equal("More than one sibling of element 'el' has the same key value, 'key1'. Key values must be unique.", ex.Message);
361361
}
362362

363363
[Fact]
@@ -374,7 +374,7 @@ public void RejectsClashingKeysEvenIfAllPairsMatch()
374374

375375
// Act/Assert
376376
var ex = Assert.Throws<InvalidOperationException>(() => GetSingleUpdatedComponent());
377-
Assert.Equal("More than one sibling has the same key value, 'key1'. Key values must be unique.", ex.Message);
377+
Assert.Equal("More than one sibling of element 'el' has the same key value, 'key1'. Key values must be unique.", ex.Message);
378378
}
379379

380380
[Fact]

0 commit comments

Comments
 (0)