{"record":{"id":"56045267bda3bfd6","repo":"louthy/language-ext","slug":"key-not-found-in-map","errorCode":null,"errorMessage":"Key not found in Map","messagePattern":"Key not found in Map","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"LanguageExt.Core/Immutable Collections/Map/Map.Internal.cs","lineNumber":598,"sourceCode":"        return SetRoot(MapModule.SetItem<OrdK, K, V>(Root, key, value));\n    }\n\n    /// <summary>\n    /// Retrieve a value from the map by key, map it to a new value,\n    /// put it back.\n    /// </summary>\n    /// <param name=\"key\">Key to set</param>\n    /// <exception cref=\"ArgumentException\">Throws ArgumentException if the item isn't found</exception>\n    /// <exception cref=\"Exception\">Throws Exception if Some returns null</exception>\n    /// <returns>New map with the mapped value</returns>\n    [Pure]\n    [MethodImpl(MethodImplOptions.AggressiveInlining)]\n    public MapInternal<OrdK, K, V> SetItem(K key, Func<V, V> Some) =>\n        isnull(key)\n            ? this\n            : match(MapModule.TryFind<OrdK, K, V>(Root, key),\n                    Some: x => SetItem(key, Some(x)),\n                    None: () => throw new ArgumentException(\"Key not found in Map\"));\n\n    /// <summary>\n    /// Atomically updates an existing item, unless it doesn't exist, in which case \n    /// it is ignored\n    /// </summary>\n    /// <remarks>Null is not allowed for a Key or a Value</remarks>\n    /// <param name=\"key\">Key</param>\n    /// <param name=\"value\">Value</param>\n    /// <exception cref=\"ArgumentNullException\">Throws ArgumentNullException the value is null</exception>\n    /// <returns>New Map with the item added</returns>\n    [Pure]\n    [MethodImpl(MethodImplOptions.AggressiveInlining)]\n    public MapInternal<OrdK, K, V> TrySetItem(K key, V value)\n    {\n        if (isnull(key)) return this;\n        return SetRoot(MapModule.TrySetItem<OrdK, K, V>(Root, key, value));\n    }\n","sourceCodeStart":580,"sourceCodeEnd":616,"githubUrl":"https://github.com/louthy/language-ext/blob/2f0e3628242889774d4141960a35671a0280051f/LanguageExt.Core/Immutable Collections/Map/Map.Internal.cs#L580-L616","documentation":"MapInternal.SetItem(key, Func<V,V> Some) updates an existing value via the supplied function but throws ArgumentException('Key not found in Map') when MapModule.TryFind returns None — i.e. the key is absent. Unlike the AddOrUpdate-style 'SetItem that ignores missing keys' variant, this overload demands the key exist. Null keys are silently ignored (returns this) rather than throwing here.","triggerScenarios":"Calling SetItem(key, updater) where key was never inserted or was already removed; key exists only in a different Map snapshot; case/type differences making the key mismatch (e.g. string casing under default Ord).","commonSituations":"Assuming SetItem acts as upsert (it does not in this overload); updating after a Remove in the same immutable pipeline; building keys by concatenation that differ from insert-time keys; culture-sensitive string keys.","solutions":["Use the Find/map API or the TrySetItem/SetItem-or-add variant if you want upsert semantics (e.g. AddOrUpdate pattern).","Check map.ContainsKey(key) (or Find) before applying the updater.","Fix the key construction so it matches the key used at insert.","Insert the key first (Add) when absence is expected, then update."],"exampleFix":"// before\nvar map2 = map.SetItem(key, v => v + 1); // throws if key absent\n// after\nvar map2 = map.Find(key)\n    .Match(Some: v => map.SetItem(key, v + 1),\n           None: () => map.Add(key, 1));","handlingStrategy":"validation","validationCode":"var map2 = map.ContainsKey(key)\n    ? map.SetItem(key, v => v + 1)\n    : map.Add(key, 1);","typeGuard":null,"tryCatchPattern":"try { var map2 = map.SetItem(key, updater); }\ncatch (ArgumentException ex) when (ex.Message == \"Key not found in Map\")\n{\n    // key absent — add or ignore\n}","preventionTips":["Never assume SetItem(key, Func<V,V>) upserts — it requires the key to exist.","Check ContainsKey/Find before updating, or use the add-or-update pattern.","Normalize string keys consistently (casing, trimming) at insert and lookup.","Re-read the map after intermediate Removes rather than reusing stale references."],"tags":["csharp","languageext","map","key-not-found"],"backgroundTag":"record-not-found","analyzedSha":"2f0e3628242889774d4141960a35671a0280051f","analyzedAt":"2026-09-15T03:31:55.716Z","contentChangedAt":"2026-09-15T03:31:55.716Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}