louthy/language-ext · error · ArgumentException
Key doesn't exist in map
Error message
Key doesn't exist in map: {change.Key} What it means
This throw fires in the TrieMap update traversal when the key is definitively absent (the node/section where it would live is empty or a terminal leaf) and the update type is SetItem. SetItem requires the key to already exist; TrySetItem returns without change instead. It is the strict-mode counterpart of the not-found-at-node case.
Solutions
- Use AddOrUpdate for insert-or-replace behavior
- Use TrySetItem to make the update a no-op for missing keys
- Add the key first (Add) then SetItem, or restructure to a single AddOrUpdate
- Log/inspect the key with FindOption to confirm presence before SetItem
Example fix
// before map = map.SetItem(missingKey, value); // after map = map.TrySetItem(missingKey, value); // no-op if absent // or map = map.AddOrUpdate(missingKey, value);
Defensive patterns
Strategy: validation
Validate before calling
if (map.ContainsKey(key)) map = map.SetItem(key, value); else map = map.Add(key, value);
Try / catch
try { map = map.SetItem(key, value); }
catch (ArgumentException) { /* key absent: add or skip */ } Prevention
- Use AddOrUpdate instead of SetItem for insert-or-replace
- Use TrySetItem for optional updates
- Check ContainsKey before strict SetItem
When it happens
Trigger: Calling map.SetItem(key, value) for a key that is not in the TrieMap at all; also reached via bulk update APIs that set the update type to SetItem.
Common situations: Assuming SetItem upserts; calling SetItem after Remove on the same key; typos or casing mismatches between the add and set call sites.
Understand the failure class
Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.
Related errors
- Key doesn't exist in map
- Key doesn't exist in map
- ArgumentOutOfRangeException
- Key not found in Map
- Key not found in set
AI-assisted analysis of louthy/language-ext@2f0e362824 (2026-09-15).
Data as JSON: /api/errors/33a7540ee4c05b6d.
Report an issue: GitHub.
Appendix: source
Thrown at LanguageExt.Core/Immutable Collections/TrieMap/TrieMap.cs:2375
return (1, new Entries(newEntryMap, newNodeMap, newItems, newNodes), default, true);
}
}
else if (Bit.Get(NodeMap, mask))
{
// var nodeIndex = Index(NodeMap, mask);
var nodeIndex = BitCount((int)NodeMap & ((int)mask - 1));
var nodeToUpdate = Nodes[nodeIndex];
var (cd, newNode, ov, ch) = nodeToUpdate.Update(env, change, hash, section.Next());
var (newNodes, _) = SetItem(Nodes, nodeIndex, newNode, env.Mutate);
return (cd, new Entries(EntryMap, NodeMap, Items, newNodes), ov, ch);
}
else
{
if (env.Type == UpdateType.SetItem)
{
// Key must already exist to set it
throw new ArgumentException($"Key doesn't exist in map: {change.Key}");
}
else if (env.Type == UpdateType.TrySetItem)
{
// Key doesn't exist, so there's nothing to set
return (0, this, default, false);
}
// var entryIndex = Index(EntryMap, mask);
var entryIndex = BitCount((int)EntryMap & ((int)mask - 1));
// var entries = Bit.Set(EntryMap, mask, true);
var entries = EntryMap | mask;
var newItems = Insert(Items, entryIndex, change);
return (1, new Entries(entries, NodeMap, newItems, Nodes), default, true);
}
}
View on GitHub (pinned to 2f0e362824)