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

  1. Guarantee app.State().Set(key, value) executes before any MustGet(key) — audit initialization/DI order.
  2. Prefer the comma-ok Get(key) accessor and handle absence explicitly on any code path where presence isn't guaranteed.
  3. Define the key once as a shared constant used by both Set and Get to eliminate typos.
  4. 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

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


AI-assisted analysis of gofiber/fiber@9a4c7e57fe (2026-08-04). Data as JSON: /data/errors/24b136683a1d1d14.json. Report an issue: GitHub.