gofiber/fiber · error
cache: unexpected raw entry type %T for key
Error message
cache: unexpected raw entry type %T for key %q
What it means
Returned by the in-memory branch of manager.getRaw when memory.Get returns a value that is not []byte. Like 143, this is an invariant violation: setRaw only stores []byte into memory, so a non-[]byte value indicates shared memory, aliasing, or a bug.
Solutions
- Ensure the cache's memory.Storage is not shared with other writers of non-[]byte values.
- Give each cache middleware instance its own memory store; do not share across apps.
- Audit test fixtures that populate memory.Storage directly.
- File a bug if reproduced in isolation.
Defensive patterns
Strategy: type-guard
Type guard
func assertRawStored(s *memory.Storage, key string) error {
v := s.Get(key)
if v == nil {
return nil
}
if _, ok := v.([]byte); !ok {
return fmt.Errorf("unexpected type %T stored under raw cache key", v)
}
return nil
} Try / catch
if value := m.memory.Get(key); value != nil {
raw, ok := value.([]byte)
if !ok {
log.Error("raw-body memory type violation:", fmt.Sprintf("%T", value))
m.memory.Delete(key)
return nil, errCacheMiss
}
return raw, nil
} Prevention
- Do not share the cache's memory.Storage with other writers.
- One cache middleware instance per app.
- Keep test fixtures type-consistent with what setRaw writes.
When it happens
Trigger: In-memory mode (cfg.Storage nil) where memory.Get(rawKey) returns a value whose type assertion to []byte fails. Requires external interference with the memory store or a bug in the cache.
Common situations: Not reachable in correct usage. Theoretically possible with a globally shared memory.Storage, manual seeding in tests, or memory corruption. Treat as a defect signal.
Related errors
- cache: unexpected entry type %T for key
- csrf: unexpected value type %T in storage
- cache: failed to delete key
- cache: failed to delete raw key
- cache: failed to get key
AI-assisted analysis of gofiber/fiber@a105acad6c (2026-08-11).
Data as JSON: /api/errors/24abc2613af04f28.
Report an issue: GitHub.
Appendix: source
Thrown at middleware/cache/manager.go:156
}
// get raw data from storage or memory
func (m *manager) getRaw(ctx context.Context, key string) ([]byte, error) {
if m.storage != nil {
raw, err := m.storage.GetWithContext(ctx, key)
if err != nil {
return nil, fmt.Errorf("cache: failed to get raw key %q from storage: %w", m.logKey(key), err)
}
if raw == nil {
return nil, errCacheMiss
}
return raw, nil
}
if value := m.memory.Get(key); value != nil {
raw, ok := value.([]byte)
if !ok {
return nil, fmt.Errorf("cache: unexpected raw entry type %T for key %q", value, m.logKey(key))
}
return raw, nil
}
return nil, errCacheMiss
}
// set data to storage or memory
func (m *manager) set(ctx context.Context, key string, it *item, exp time.Duration) error {
if m.storage != nil {
raw, err := it.MarshalMsg(nil)
if err != nil {
m.release(it)
return fmt.Errorf("cache: failed to marshal key %q: %w", m.logKey(key), err)
}
if err := m.storage.SetWithContext(ctx, key, raw, exp); err != nil {
m.release(it)
return fmt.Errorf("cache: failed to store key %q: %w", m.logKey(key), err)View on GitHub (pinned to a105acad6c)