gofiber/fiber · critical
state: dependency not found!
Error message
state: dependency not found!
What it means
Raised by (*State).MustGet (state.go:51) when State.Get returns ok==false for the requested key — i.e., no value was ever Store/Set under that key (or it was Delete'd / Reset'd). State is a sync.Map-backed key-value store on App (state.go:21-24); MustGet is the convenience accessor that panics instead of returning the comma-ok form. This is the untyped any accessor and is distinct from the generic MustGetState (state.go:118), which additionally panics on type-assertion failure.
Source
Thrown at state.go:51
}
// Set sets a key-value pair in the State.
func (s *State) Set(key string, value any) {
s.dependencies.Store(key, value)
}
// Get retrieves a value from the State.
func (s *State) Get(key string) (any, bool) {
return s.dependencies.Load(key)
}
// MustGet retrieves a value from the State and panics if the key is not found.
func (s *State) MustGet(key string) any {
if dep, ok := s.Get(key); ok {
return dep
}
panic("state: dependency not found!")
}
// Has checks if a key is present in the State.
// It returns a boolean indicating if the key is present.
func (s *State) Has(key string) bool {
_, ok := s.Get(key)
return ok
}
// Delete removes a key-value pair from the State.
func (s *State) Delete(key string) {
s.dependencies.Delete(key)
}
// Reset resets the State by removing all keys.
func (s *State) Reset() {
s.dependencies.Clear()
}View on GitHub (pinned to 9a4c7e57fe)
Solutions
- Guarantee app.State().Set(key, value) executes before any MustGet(key) — audit initialization/DI order.
- Prefer the comma-ok Get(key) accessor and handle absence explicitly on any code path where presence isn't guaranteed.
- Define the key once as a shared constant used by both Set and Get to eliminate typos.
- In tests, seed app.State().Set(...) before exercising the handler that calls MustGet.
Example fix
// before — panics if "db" was never set
conn := app.State().MustGet("db").(*sql.DB)
// after — handle absence explicitly
v, ok := app.State().Get("db")
if !ok {
return fmt.Errorf("dependency %q not registered", "db")
}
conn := v.(*sql.DB) Defensive patterns
Strategy: validation
Validate before calling
const depKey = "db" // shared with the Set call site
if !app.State().Has(depKey) {
return fmt.Errorf("dependency %q not registered", depKey)
}
v := app.State().MustGet(depKey) Try / catch
defer func() {
if r := recover(); r != nil {
if msg, _ := r.(string); msg == "state: dependency not found!" {
// handle missing dependency without crashing the request
return
}
panic(r) // re-raise unrelated panics
}
}()
v := app.State().MustGet("db") Prevention
- Centralize every State key as a constant used by both Set and Get.
- Prefer Get (comma-ok) over MustGet on any path where presence isn't structurally guaranteed.
- Seed app.State() in tests before invoking handlers that call MustGet.
- Review initialization order so producers always Set before consumers read.
When it happens
Trigger: Call app.State().MustGet("db") when "db" was never stored via app.State().Set("db", conn) — for example a consumer handler/initializer runs before the producer Set, the key string differs (case or typo), or State was Reset/cleared in a shutdown hook before a late lookup.
Common situations: Dependency-injection ordering bugs (consumer registered before producer); typo between the Set key and the Get key; tests that construct an App via newState() but forget to seed State; calling MustGet inside a handler after Reset ran during graceful shutdown.
Related errors
- state: service not found!
- runtime.Goexit() called in handler or server panic
- failed to type-assert to *Middleware
- client panic: %v
- favicon: read limited: %w
AI-assisted analysis of gofiber/fiber@9a4c7e57fe (2026-08-04).
Data as JSON: /data/errors/24b136683a1d1d14.json.
Report an issue: GitHub.