{"id":"24abc2613af04f28","repo":"gofiber/fiber","slug":"cache-unexpected-raw-entry-type-t-for-key-q","errorCode":null,"errorMessage":"cache: unexpected raw entry type %T for key %q","messagePattern":"cache: unexpected raw entry type %T for key %q","errorType":"http","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"middleware/cache/manager.go","lineNumber":156,"sourceCode":"}\n\n// get raw data from storage or memory\nfunc (m *manager) getRaw(ctx context.Context, key string) ([]byte, error) {\n\tif m.storage != nil {\n\t\traw, err := m.storage.GetWithContext(ctx, key)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"cache: failed to get raw key %q from storage: %w\", m.logKey(key), err)\n\t\t}\n\t\tif raw == nil {\n\t\t\treturn nil, errCacheMiss\n\t\t}\n\t\treturn raw, nil\n\t}\n\n\tif value := m.memory.Get(key); value != nil {\n\t\traw, ok := value.([]byte)\n\t\tif !ok {\n\t\t\treturn nil, fmt.Errorf(\"cache: unexpected raw entry type %T for key %q\", value, m.logKey(key))\n\t\t}\n\t\treturn raw, nil\n\t}\n\n\treturn nil, errCacheMiss\n}\n\n// set data to storage or memory\nfunc (m *manager) set(ctx context.Context, key string, it *item, exp time.Duration) error {\n\tif m.storage != nil {\n\t\traw, err := it.MarshalMsg(nil)\n\t\tif err != nil {\n\t\t\tm.release(it)\n\t\t\treturn fmt.Errorf(\"cache: failed to marshal key %q: %w\", m.logKey(key), err)\n\t\t}\n\t\tif err := m.storage.SetWithContext(ctx, key, raw, exp); err != nil {\n\t\t\tm.release(it)\n\t\t\treturn fmt.Errorf(\"cache: failed to store key %q: %w\", m.logKey(key), err)","sourceCodeStart":138,"sourceCodeEnd":174,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/middleware/cache/manager.go#L138-L174","documentation":"Thrown at middleware/cache/manager.go:156 on the in-memory fallback path of getRaw: memory.Get(key) returned a non-nil value that is not []byte. The raw path expects byte slices; any other Go type indicates cross-middleware contamination of the shared memory store.","triggerScenarios":"Same class as error 142: only fires when the cache's internal memory.Storage is shared with another component that stored a non-[]byte value under a colliding key. Does not occur in default wiring where each manager owns its private store.","commonSituations":"Test harness reusing one memory.Storage across cache + another middleware; custom fork injecting a shared store; deliberate key reuse across components in development.","solutions":["Give the cache middleware its own memory.Storage (pass nil for Storage so it allocates privately).","Namespace keys with distinct prefixes if sharing a store is unavoidable.","Reset shared memory.Storage between tests to avoid cross-test leakage.","Audit every .Set call on the shared store to confirm only []byte values land on raw-path keys."],"exampleFix":"// before: shared store serves both msgp *item and raw []byte paths\nstore := memory.New()\nm := newManager(store, false)\n\n// after: private store per manager\nm := newManager(nil, false) // owns its memory.Storage; type collisions impossible","handlingStrategy":"type-guard","validationCode":"// Don't share the cache's memory store with components that store non-[]byte values.\nif cfg.Storage == nil { /* private memory store - safe */ }","typeGuard":"func asBytes(v any) ([]byte, bool) {\n    b, ok := v.([]byte)\n    return b, ok\n}","tryCatchPattern":"v := m.memory.Get(key)\nif v != nil {\n    raw, ok := v.([]byte)\n    if !ok {\n        log.Printf(\"unexpected raw type %T\", v)\n        return nil, errCacheMiss\n    }\n    _ = raw\n}","preventionTips":["Allocate a private memory.Storage per middleware (Storage: nil).","Namespace keys when sharing is unavoidable.","Reset memory stores between tests.","Audit .Set callers on shared stores for type correctness."],"tags":["cache","memory","type-safety","fiber"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}