Add the key in the message on throwing a KeyNotFoundException (#282)
authorRogier van der Hee <rogihee@users.noreply.github.com>
Tue, 9 Aug 2016 10:37:35 +0000 (12:37 +0200)
committerRui Marinho <me@ruimarinho.net>
Tue, 9 Aug 2016 10:37:35 +0000 (11:37 +0100)
* Add the key in the message on throwing a KeyNotFoundException for trying to access an invalid key in the ResourceDictionary. This helps a lot in tracking down what resource is actually missing.

* Fix test build, use C# 6 string interpolation

Xamarin.Forms.Core.UnitTests/ResourceDictionaryTests.cs
Xamarin.Forms.Core/ResourceDictionary.cs

index d422922..c4e6bbf 100644 (file)
@@ -252,5 +252,14 @@ namespace Xamarin.Forms.Core.UnitTests
                        elt.Parent = parent;
                        Assert.Fail ();
                }
-       }
+
+        [Test]
+        public void ShowKeyInExceptionIfNotFound()
+        {
+            var rd = new ResourceDictionary();
+            rd.Add("foo", "bar");
+            var ex = Assert.Throws<KeyNotFoundException>(() => { var foo = rd["test_invalid_key"]; });
+            Assert.That(ex.Message, Is.StringContaining("test_invalid_key"));
+        }
+    }
 }
\ No newline at end of file
index 791c6e5..0747eaa 100644 (file)
@@ -98,7 +98,7 @@ namespace Xamarin.Forms
                                        return _innerDictionary[index];
                                if (_mergedInstance != null && _mergedInstance.ContainsKey(index))
                                        return _mergedInstance[index];
-                               throw new KeyNotFoundException();
+                               throw new KeyNotFoundException($"The resource '{index}' is not present in the dictionary.");
                        }
                        set
                        {