Include key when throwing KeyNotFoundException in indexer (#34759)
authorLennart Brüggemann <lbrueggemann@outlook.com>
Fri, 10 Apr 2020 12:35:59 +0000 (14:35 +0200)
committerGitHub <noreply@github.com>
Fri, 10 Apr 2020 12:35:59 +0000 (08:35 -0400)
* Include key when throwing KeyNotFoundException in indexer

* Add quotes around replacement markers to be consistent with other strings

src/libraries/System.Collections.Immutable/src/Resources/Strings.resx
src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.cs
src/libraries/System.Collections.Immutable/tests/ImmutableDictionaryTest.cs
src/libraries/System.Collections.Immutable/tests/ImmutableSortedDictionaryTest.cs

index 7bdc72c..c92a6d0 100644 (file)
@@ -79,7 +79,7 @@
     <value>Collection was modified; enumeration operation may not execute.</value>
   </data>
   <data name="DuplicateKey" xml:space="preserve">
-    <value>An element with the same key but a different value already exists. Key: {0}</value>
+    <value>An element with the same key but a different value already exists. Key: '{0}'</value>
   </data>
   <data name="InvalidEmptyOperation" xml:space="preserve">
     <value>This operation does not apply to an empty instance.</value>
index 1d5241c..6864319 100644 (file)
@@ -254,7 +254,7 @@ namespace System.Collections.Immutable
                     return value;
                 }
 
-                throw new KeyNotFoundException();
+                throw new KeyNotFoundException(SR.Format(SR.Arg_KeyNotFoundWithKey, key.ToString()));
             }
         }
 
index d57ea5f..a2127c0 100644 (file)
@@ -377,6 +377,15 @@ namespace System.Collections.Immutable.Tests
             Assert.True(clearedDictionary.ContainsKey("A"));
         }
 
+        [Fact]
+        public void Indexer_KeyNotFoundException_ContainsKeyInMessage()
+        {
+            var map = ImmutableDictionary.Create<string, string>()
+                .Add("a", "1").Add("b", "2");
+            var exception = Assert.Throws<KeyNotFoundException>(() => map["c"]);
+            Assert.Contains("'c'", exception.Message);
+        }
+
         protected override IImmutableDictionary<TKey, TValue> Empty<TKey, TValue>()
         {
             return ImmutableDictionaryTest.Empty<TKey, TValue>();
index e33c957..e972ece 100644 (file)
@@ -506,6 +506,15 @@ namespace System.Collections.Immutable.Tests
             Assert.Throws<KeyNotFoundException>(() => dictionary.ValueRef("c"));
         }
 
+        [Fact]
+        public void Indexer_KeyNotFoundException_ContainsKeyInMessage()
+        {
+            var map = ImmutableSortedDictionary.Create<string, string>()
+                .Add("a", "1").Add("b", "2");
+            var exception = Assert.Throws<KeyNotFoundException>(() => map["c"]);
+            Assert.Contains("'c'", exception.Message);
+        }
+
         protected override IImmutableDictionary<TKey, TValue> Empty<TKey, TValue>()
         {
             return ImmutableSortedDictionaryTest.Empty<TKey, TValue>();