{"id":"80f29b79182f4c8a","repo":"labstack/echo","slug":"errnonexistentkey","errorCode":"ErrNonExistentKey","errorMessage":"non existent key","messagePattern":"non existent key","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"context_generic.go","lineNumber":9,"sourceCode":"// SPDX-License-Identifier: MIT\n// SPDX-FileCopyrightText: © 2015 LabStack LLC and Echo contributors\n\npackage echo\n\nimport \"errors\"\n\n// ErrNonExistentKey is error that is returned when key does not exist\nvar ErrNonExistentKey = errors.New(\"non existent key\")\n\n// ErrInvalidKeyType is error that is returned when the value is not castable to expected type.\nvar ErrInvalidKeyType = errors.New(\"invalid key type\")\n\n// ContextGet retrieves a value from the context store or ErrNonExistentKey error the key is missing.\n// Returns ErrInvalidKeyType error if the value is not castable to type T.\nfunc ContextGet[T any](c *Context, key string) (T, error) {\n\tc.lock.RLock()\n\tdefer c.lock.RUnlock()\n\n\tval, ok := c.store[key]\n\tif !ok {\n\t\tvar zero T\n\t\treturn zero, ErrNonExistentKey\n\t}\n\n\ttyped, ok := val.(T)\n\tif !ok {","sourceCodeStart":1,"sourceCodeEnd":27,"githubUrl":"https://github.com/labstack/echo/blob/05489dc1730161df26b72d1ae2a3ba6fb8178fc7/context_generic.go#L1-L27","documentation":"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.","triggerScenarios":"Calling echo.ContextGet[SomeType](c, \"user\") when no handler or middleware has previously called c.Set(\"user\", value) for this request.","commonSituations":"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).","solutions":["Use echo.ContextGetOr[T](c, key, defaultValue) which returns the default instead of an error when the key is missing","Ensure the middleware/handler that calls c.Set runs before the one that reads it (check e.Use order)","Check errors.Is(err, echo.ErrNonExistentKey) to distinguish missing-key from type errors"],"exampleFix":"// before\nval, err := echo.ContextGet[User](c, \"user\")\nif err != nil { /* both missing and type errors mixed */ }\n\n// after\nval, err := echo.ContextGetOr[User](c, \"user\", User{})\nif err != nil { /* only ErrInvalidKeyType remains */ }","handlingStrategy":"validation","validationCode":"// Check key existence before typed retrieval using the untyped API\nif c.Get(\"user\") == nil {\n    // key not set; handle accordingly\n} else {\n    u, _ := echo.ContextGet[User](c, \"user\")\n}","typeGuard":null,"tryCatchPattern":"val, err := echo.ContextGet[User](c, \"user\")\nif err != nil {\n    if errors.Is(err, echo.ErrNonExistentKey) {\n        // key missing — use default or redirect\n    }\n    return err\n}","preventionTips":["Prefer echo.ContextGetOr[T](c, key, default) when a missing key is a normal case","Verify middleware ordering: the writer (c.Set) must run before the reader","Use a single shared constant for context keys to avoid typos"],"tags":["context-store","generics","key-lookup"],"analyzedSha":"05489dc1730161df26b72d1ae2a3ba6fb8178fc7","analyzedAt":"2026-08-04T21:32:47.783Z","schemaVersion":2}