labstack/echo · error

ErrInvalidKeyType

ErrInvalidKeyType

Error message

invalid key type

What it means

ErrInvalidKeyType is returned by the generic ContextGet[T] / ContextGetOr[T] when the key exists in the context store but the stored value cannot be type-asserted to T. This happens when one part of the code stores a value of one type and another reads it with a different generic type parameter.

Source

Thrown at context_generic.go:12

// 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 {
		var zero T
		return zero, ErrInvalidKeyType
	}

View on GitHub (pinned to 05489dc173)

Solutions

  1. Ensure the type parameter T in ContextGet[T] exactly matches the type passed to c.Set (including pointer-ness)
  2. Use a typed constant/helper for the key and type to keep writer and reader in sync
  3. Check errors.Is(err, echo.ErrInvalidKeyType) to handle this specifically

Example fix

// before — type mismatch
c.Set("user", &User{})
u, _ := echo.ContextGet[User](c, "user") // ErrInvalidKeyType

// after
c.Set("user", &User{})
u, _ := echo.ContextGet[*User](c, "user")
Defensive patterns

Strategy: type-guard

Validate before calling

// Inspect the stored type before asserting
raw := c.Get("user")
if raw != nil {
    fmt.Printf("stored type: %T\n", raw)
}

Type guard

// Centralize the get with a consistent type
func GetUser(c echo.Context) (*User, error) {
    return echo.ContextGet[*User](c, "user")
}
// Always call c.Set("user", &User{...}) to match

Try / catch

val, err := echo.ContextGet[User](c, "user")
if err != nil {
    if errors.Is(err, echo.ErrInvalidKeyType) {
        log.Printf("type mismatch for key 'user': stored %T", c.Get("user"))
    }
    return val, err
}

Prevention

When it happens

Trigger: Calling c.Set("data", someString) in one middleware and echo.ContextGet[int](c, "data") in another. Or storing a *User and reading with echo.ContextGet[User].

Common situations: Type drift between writer and reader (e.g. refactoring a struct from value to pointer but not updating all readers). Two middlewares using the same key string for different value types.

Related errors


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