Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Add the key in the message on throwing a KeyNotFoundException (#282)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rogihee authored and rmarinho committed Aug 9, 2016
1 parent 0a73c0f commit 43e90a3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 10 additions & 1 deletion Xamarin.Forms.Core.UnitTests/ResourceDictionaryTests.cs
Expand Up @@ -252,5 +252,14 @@ public void MultiLevelMerge ()
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"));
}
}
}
2 changes: 1 addition & 1 deletion Xamarin.Forms.Core/ResourceDictionary.cs
Expand Up @@ -98,7 +98,7 @@ public bool ContainsKey(string key)
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
{
Expand Down

0 comments on commit 43e90a3

Please sign in to comment.