gofiber/fiber · error
state: dependency not found!
Error message
state: dependency not found!
What it means
State.MustGet returns a dependency by key and panics if the key is not present in the underlying sync.Map. State is Fiber's app-level key-value store for global dependencies (set via Set/RegisterAppDependency). MustGet is the convenience variant for cases where the caller is certain the key exists; the panic signals a broken initialization contract — a dependency was expected but never registered.
Solutions
- Register the dependency with State.Set before any code path that calls MustGet.
- Prefer the non-panicking State.Get and handle the missing case explicitly when absence is possible.
- Centralize dependency keys as constants to avoid typos.
Example fix
// before
func handler(c fiber.Ctx) error {
db := c.App().State().MustGet("database") // panics if unregistered
...
}
// after
func handler(c fiber.Ctx) error {
dep, ok := c.App().State().Get("database")
if !ok {
return fiber.NewError(fiber.StatusServiceUnavailable, "database unavailable")
}
db := dep.(*sql.DB)
...
} Defensive patterns
Strategy: validation
Validate before calling
func getDependencyOr(s *fiber.State, key string) (any, error) {
dep, ok := s.Get(key)
if !ok {
return nil, fmt.Errorf("dependency %q not registered", key)
}
return dep, nil
}
// dep, err := getDependencyOr(app.State(), "db"); if err != nil { handle } Type guard
func stateHasKey(s *fiber.State, key string) bool { _, ok := s.Get(key); return ok } Prevention
- Prefer State.Get over MustGet whenever absence is possible; reserve MustGet for invariants you guarantee at construction.
- Register all dependencies in a single bootstrap phase before mounting handlers.
- Use constants for dependency keys to prevent drift between Set and MustGet sites.
When it happens
Trigger: app.state.MustGet("db") before any Set("db", ...) call; reading a dependency registered on a different App's State; a key typo between the Set and MustGet sites.
Common situations: A dependency registered conditionally (e.g. only in non-test builds) that MustGet assumes always exists; a key renamed in one place but not the other; accessing a dependency during package init or before the registration hook ran.
Related errors
- state: service not found!
- add: invalid http method
- ErrUpstreamHostBlocked
- ErrUpstreamHostInvalid
- missing handler/middleware in route
AI-assisted analysis of gofiber/fiber@a105acad6c (2026-08-11).
Data as JSON: /api/errors/24b136683a1d1d14.
Report an issue: GitHub.
Appendix: 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 a105acad6c)