labstack/echo · warning

ErrNonExistentKey

ErrNonExistentKey

Error message

non existent key

What it means

ErrNonExistentKey is returned by the generic ContextGet[T] function when the requested key is not present in the per-request context store (c.store map). ContextGet is the typed retrieval counterpart to Context.Set; it distinguishes 'key missing' from 'key present but wrong type' by returning this sentinel versus ErrInvalidKeyType.

Source

Thrown at context_generic.go:9

// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: © 2015 LabStack LLC and Echo contributors

package echo

import "errors"

// ErrNonExistentKey is error that is returned when key does not exist
var ErrNonExistentKey = errors.New("non existent key")

// ErrInvalidKeyType is error that is returned when the value is not castable to expected type.
var ErrInvalidKeyType = errors.New("invalid key type")

// ContextGet retrieves a value from the context store or ErrNonExistentKey error the key is missing.
// Returns ErrInvalidKeyType error if the value is not castable to type T.
func ContextGet[T any](c *Context, key string) (T, error) {
	c.lock.RLock()
	defer c.lock.RUnlock()

	val, ok := c.store[key]
	if !ok {
		var zero T
		return zero, ErrNonExistentKey
	}

	typed, ok := val.(T)
	if !ok {

View on GitHub (pinned to 05489dc173)

Solutions

  1. Use echo.ContextGetOr[T](c, key, defaultValue) which returns the default instead of an error when the key is missing
  2. Ensure the middleware/handler that calls c.Set runs before the one that reads it (check e.Use order)
  3. Check errors.Is(err, echo.ErrNonExistentKey) to distinguish missing-key from type errors

Example fix

// before
val, err := echo.ContextGet[User](c, "user")
if err != nil { /* both missing and type errors mixed */ }

// after
val, err := echo.ContextGetOr[User](c, "user", User{})
if err != nil { /* only ErrInvalidKeyType remains */ }
Defensive patterns

Strategy: validation

Validate before calling

// Check key existence before typed retrieval using the untyped API
if c.Get("user") == nil {
    // key not set; handle accordingly
} else {
    u, _ := echo.ContextGet[User](c, "user")
}

Try / catch

val, err := echo.ContextGet[User](c, "user")
if err != nil {
    if errors.Is(err, echo.ErrNonExistentKey) {
        // key missing — use default or redirect
    }
    return err
}

Prevention

When it happens

Trigger: Calling echo.ContextGet[SomeType](c, "user") when no handler or middleware has previously called c.Set("user", value) for this request.

Common situations: Middleware ordering: a handler reads a context value before the middleware that sets it runs. Renaming a context key so old readers miss it. Forgetting to set a value in a code path (e.g. optional auth).

Related errors


AI-assisted analysis of labstack/echo@05489dc173 (2026-08-04). Data as JSON: /data/errors/80f29b79182f4c8a.json. Report an issue: GitHub.