{"record":{"id":"19b09a751b6c54f9","repo":"ory/hydra","slug":"errkeydoesnotexist","errorCode":"ErrKeyDoesNotExist","errorMessage":"key is not present in map","messagePattern":"key is not present in map","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"oryx/mapx/type_assert.go","lineNumber":14,"sourceCode":"// Copyright © 2023 Ory Corp\n// SPDX-License-Identifier: Apache-2.0\n\npackage mapx\n\nimport (\n\t\"encoding/json\"\n\t\"errors\"\n\t\"math\"\n\t\"time\"\n)\n\n// ErrKeyDoesNotExist is returned when the key does not exist in the map.\nvar ErrKeyDoesNotExist = errors.New(\"key is not present in map\")\n\n// ErrKeyCanNotBeTypeAsserted is returned when the key can not be type asserted.\nvar ErrKeyCanNotBeTypeAsserted = errors.New(\"key could not be type asserted\")\n\n// GetString returns a string for a given key in values.\nfunc GetString[K comparable](values map[K]any, key K) (string, error) {\n\tif v, ok := values[key]; !ok {\n\t\treturn \"\", ErrKeyDoesNotExist\n\t} else if sv, ok := v.(string); !ok {\n\t\treturn \"\", ErrKeyCanNotBeTypeAsserted\n\t} else {\n\t\treturn sv, nil\n\t}\n}\n\n// GetStringSlice returns a string slice for a given key in values.\nfunc GetStringSlice[K comparable](values map[K]any, key K) ([]string, error) {\n\tv, ok := values[key]","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/ory/hydra/blob/4174065ffb052799890f7480f5360a877a67ffc1/oryx/mapx/type_assert.go#L1-L32","documentation":"ErrKeyDoesNotExist is the sentinel error returned by mapx typed getters (GetString, GetStringSlice, GetTime, GetInt64, GetFloat64) when the requested key is simply absent from the map. It is distinct from ErrKeyCanNotBeTypeAsserted, which fires when the key exists but holds a different type. It indicates a lookup miss, not a type problem.","triggerScenarios":"Calling any mapx.Get* function with a key not present in the map, e.g. mapx.GetString(m, \"missing\"), or reading config/label maps with keys spelled differently from how they were stored.","commonSituations":"Typo'd config keys or env-derived map keys, case-sensitivity mismatches (\"Key\" vs \"key\"), schema changes where an upstream producer stopped emitting a field, reading optional map entries without a default.","solutions":["Check the key exists (or print the map's keys) and fix the key spelling/casing.","Use a presence check before the typed getter, or add a fallback default value.","If the key is genuinely optional, treat this sentinel as an expected branch: errors.Is(err, mapx.ErrKeyDoesNotExist).","Verify the producer of the map actually sets this key (schema/config validation upstream)."],"exampleFix":"// before\nv, err := mapx.GetString(m, \"retries\") // panics into error path if absent\n// after\nv, err := mapx.GetString(m, \"retries\")\nif err != nil {\n\tif errors.Is(err, mapx.ErrKeyDoesNotExist) {\n\t\tv = \"3\" // default\n\t} else {\n\t\treturn err\n\t}\n}","handlingStrategy":"type-guard","validationCode":"if v, ok := any(m[\"retries\"]).(*string); !ok || v == nil {\n\t// decide: default, or surface a config error before calling the getter\n}","typeGuard":"func keyExists[K comparable](m map[K]any, key K) bool {\n\t_, ok := m[key]\n\treturn ok\n}","tryCatchPattern":"v, err := mapx.GetString(m, \"retries\")\nswitch {\ncase errors.Is(err, mapx.ErrKeyDoesNotExist):\n\tv = \"3\" // apply default for optional key\ncase errors.Is(err, mapx.ErrKeyCanNotBeTypeAsserted):\n\treturn fmt.Errorf(\"config key 'retries' has wrong type\")\ncase err != nil:\n\treturn err\n}","preventionTips":["Always check errors.Is against ErrKeyDoesNotExist vs ErrKeyCanNotBeTypeAsserted — they mean different fixes","Normalize key casing when building maps from config/env","Keep a schema list of expected keys and validate maps at startup","Provide defaults for optional keys instead of letting the getter fail"],"tags":["maps","type-assertion","sentinel-error"],"backgroundTag":"missing-map-key","analyzedSha":"4174065ffb052799890f7480f5360a877a67ffc1","analyzedAt":"2026-09-03T14:52:41.581Z","contentChangedAt":"2026-09-03T14:52:41.581Z","schemaVersion":2},"datasetVersion":"2026-09-10T17:17:09.494Z"}