ory/hydra · warning

ErrKeyDoesNotExist

ErrKeyDoesNotExist

Error message

key is not present in map

What it means

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.

Source

Thrown at oryx/mapx/type_assert.go:14

// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package mapx

import (
	"encoding/json"
	"errors"
	"math"
	"time"
)

// ErrKeyDoesNotExist is returned when the key does not exist in the map.
var ErrKeyDoesNotExist = errors.New("key is not present in map")

// ErrKeyCanNotBeTypeAsserted is returned when the key can not be type asserted.
var ErrKeyCanNotBeTypeAsserted = errors.New("key could not be type asserted")

// GetString returns a string for a given key in values.
func GetString[K comparable](values map[K]any, key K) (string, error) {
	if v, ok := values[key]; !ok {
		return "", ErrKeyDoesNotExist
	} else if sv, ok := v.(string); !ok {
		return "", ErrKeyCanNotBeTypeAsserted
	} else {
		return sv, nil
	}
}

// GetStringSlice returns a string slice for a given key in values.
func GetStringSlice[K comparable](values map[K]any, key K) ([]string, error) {
	v, ok := values[key]

View on GitHub (pinned to 4174065ffb)

Solutions

  1. Check the key exists (or print the map's keys) and fix the key spelling/casing.
  2. Use a presence check before the typed getter, or add a fallback default value.
  3. If the key is genuinely optional, treat this sentinel as an expected branch: errors.Is(err, mapx.ErrKeyDoesNotExist).
  4. Verify the producer of the map actually sets this key (schema/config validation upstream).

Example fix

// before
v, err := mapx.GetString(m, "retries") // panics into error path if absent
// after
v, err := mapx.GetString(m, "retries")
if err != nil {
	if errors.Is(err, mapx.ErrKeyDoesNotExist) {
		v = "3" // default
	} else {
		return err
	}
}
Defensive patterns

Strategy: type-guard

Validate before calling

if v, ok := any(m["retries"]).(*string); !ok || v == nil {
	// decide: default, or surface a config error before calling the getter
}

Type guard

func keyExists[K comparable](m map[K]any, key K) bool {
	_, ok := m[key]
	return ok
}

Try / catch

v, err := mapx.GetString(m, "retries")
switch {
case errors.Is(err, mapx.ErrKeyDoesNotExist):
	v = "3" // apply default for optional key
case errors.Is(err, mapx.ErrKeyCanNotBeTypeAsserted):
	return fmt.Errorf("config key 'retries' has wrong type")
case err != nil:
	return err
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of ory/hydra@4174065ffb (2026-09-03). Data as JSON: /api/errors/19b09a751b6c54f9. Report an issue: GitHub.