From f004abe248da79dac220f72427b88f6456daa1c2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Lennart=20Br=C3=BCggemann?= Date: Fri, 10 Apr 2020 14:35:59 +0200 Subject: [PATCH] Include key when throwing KeyNotFoundException in indexer (#34759) * Include key when throwing KeyNotFoundException in indexer * Add quotes around replacement markers to be consistent with other strings --- .../System.Collections.Immutable/src/Resources/Strings.resx | 2 +- .../src/System/Collections/Immutable/ImmutableDictionary_2.cs | 2 +- .../tests/ImmutableDictionaryTest.cs | 9 +++++++++ .../tests/ImmutableSortedDictionaryTest.cs | 9 +++++++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Collections.Immutable/src/Resources/Strings.resx b/src/libraries/System.Collections.Immutable/src/Resources/Strings.resx index 7bdc72c..c92a6d0 100644 --- a/src/libraries/System.Collections.Immutable/src/Resources/Strings.resx +++ b/src/libraries/System.Collections.Immutable/src/Resources/Strings.resx @@ -79,7 +79,7 @@ Collection was modified; enumeration operation may not execute. - An element with the same key but a different value already exists. Key: {0} + An element with the same key but a different value already exists. Key: '{0}' This operation does not apply to an empty instance. diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.cs index 1d5241c..6864319 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.cs @@ -254,7 +254,7 @@ namespace System.Collections.Immutable return value; } - throw new KeyNotFoundException(); + throw new KeyNotFoundException(SR.Format(SR.Arg_KeyNotFoundWithKey, key.ToString())); } } diff --git a/src/libraries/System.Collections.Immutable/tests/ImmutableDictionaryTest.cs b/src/libraries/System.Collections.Immutable/tests/ImmutableDictionaryTest.cs index d57ea5f..a2127c0 100644 --- a/src/libraries/System.Collections.Immutable/tests/ImmutableDictionaryTest.cs +++ b/src/libraries/System.Collections.Immutable/tests/ImmutableDictionaryTest.cs @@ -377,6 +377,15 @@ namespace System.Collections.Immutable.Tests Assert.True(clearedDictionary.ContainsKey("A")); } + [Fact] + public void Indexer_KeyNotFoundException_ContainsKeyInMessage() + { + var map = ImmutableDictionary.Create() + .Add("a", "1").Add("b", "2"); + var exception = Assert.Throws(() => map["c"]); + Assert.Contains("'c'", exception.Message); + } + protected override IImmutableDictionary Empty() { return ImmutableDictionaryTest.Empty(); diff --git a/src/libraries/System.Collections.Immutable/tests/ImmutableSortedDictionaryTest.cs b/src/libraries/System.Collections.Immutable/tests/ImmutableSortedDictionaryTest.cs index e33c957..e972ece 100644 --- a/src/libraries/System.Collections.Immutable/tests/ImmutableSortedDictionaryTest.cs +++ b/src/libraries/System.Collections.Immutable/tests/ImmutableSortedDictionaryTest.cs @@ -506,6 +506,15 @@ namespace System.Collections.Immutable.Tests Assert.Throws(() => dictionary.ValueRef("c")); } + [Fact] + public void Indexer_KeyNotFoundException_ContainsKeyInMessage() + { + var map = ImmutableSortedDictionary.Create() + .Add("a", "1").Add("b", "2"); + var exception = Assert.Throws(() => map["c"]); + Assert.Contains("'c'", exception.Message); + } + protected override IImmutableDictionary Empty() { return ImmutableSortedDictionaryTest.Empty(); -- 2.7.4