Runtime: ImmutableDictionary<K, V> should include key when throwing KeyNotFoundException

Created on 8 Apr 2020  路  7Comments  路  Source: dotnet/runtime

This looks like a regression because we merged a fix for that. Dictionary<K, V> still works as expected though.

Repro

Target .NET Core 3.1 and use the in-box version of immutable collections.

```C#
try
{
var d = ImmutableDictionary.Create();
Console.WriteLine(d["Immo"]);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}


### Expected output

The given key 'Immo' was not present in the dictionary.


### Actual output

The given key was not present in the dictionary.
```

area-System.Collections bug easy up-for-grabs

Most helpful comment

Re exposing info, it would be nice to consistently use quotes around replacement markers, so that "obfuscate between quotes before writing to log" is a potentially viable strategy. This one violates that: <value>An item with the same key has already been added. Key: {0}</value>

All 7 comments

Tagging @eiriktsarpalis as an area owner

This looks like a regression because we merged a fix for that. Dictionary still works as expected though.

That fix was about a duplicate key rather than about a non-existent key. The latter is just throwing a KeyNotFoundException, with the default message for that exception type:
https://github.com/akoeplinger/corefx/blob/25b88a4034e05ff189b9ea7b187dfcdb7286fc47/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary%602.cs#L258

Gotcha. I assume this means you've got no concerns with fixing this too though?

No concerns (other than the standard ones about exposing info, which we generally don't believe outweighs the benefits). cc: @danmosemsft who also cares a lot about this kind of thing.

(that said, it'd be nice to audit all such errors as part of the fix, rather than doing it piecemeal one exception at a time)

This seems to be the only place we throw KNFE without a key. Aside from 3 in JsonElement.cs which I can't speak to.

Since the adding-duplicate case was mentioned, there may be some unrelated cleanup that could be done there: some collections that could use the more informational forms.

  <data name="Argument_AddingDuplicate" xml:space="preserve">
    <value>An item with the same key has already been added.</value>
  </data>
  <data name="Argument_AddingDuplicate__" xml:space="preserve">
    <value>Item has already been added. Key in dictionary: '{0}'  Key being added: '{1}'</value>
  </data>
  <data name="Argument_AddingDuplicateWithKey" xml:space="preserve">
    <value>An item with the same key has already been added. Key: {0}</value>
  </data>
  <data name="Argument_AddingDuplicate_OldAndNewKeys" xml:space="preserve">
    <value>Item has already been added. Key in dictionary: '{0}'  Key being added: '{1}'</value>
  </data>

Re exposing info, it would be nice to consistently use quotes around replacement markers, so that "obfuscate between quotes before writing to log" is a potentially viable strategy. This one violates that: <value>An item with the same key has already been added. Key: {0}</value>

Was this page helpful?
0 / 5 - 0 ratings