{"record":{"id":"24b136683a1d1d14","repo":"gofiber/fiber","slug":"state-dependency-not-found","errorCode":null,"errorMessage":"state: dependency not found!","messagePattern":"state: dependency not found!","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"state.go","lineNumber":51,"sourceCode":"}\n\n// Set sets a key-value pair in the State.\nfunc (s *State) Set(key string, value any) {\n\ts.dependencies.Store(key, value)\n}\n\n// Get retrieves a value from the State.\nfunc (s *State) Get(key string) (any, bool) {\n\treturn s.dependencies.Load(key)\n}\n\n// MustGet retrieves a value from the State and panics if the key is not found.\nfunc (s *State) MustGet(key string) any {\n\tif dep, ok := s.Get(key); ok {\n\t\treturn dep\n\t}\n\n\tpanic(\"state: dependency not found!\")\n}\n\n// Has checks if a key is present in the State.\n// It returns a boolean indicating if the key is present.\nfunc (s *State) Has(key string) bool {\n\t_, ok := s.Get(key)\n\treturn ok\n}\n\n// Delete removes a key-value pair from the State.\nfunc (s *State) Delete(key string) {\n\ts.dependencies.Delete(key)\n}\n\n// Reset resets the State by removing all keys.\nfunc (s *State) Reset() {\n\ts.dependencies.Clear()\n}","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/gofiber/fiber/blob/a105acad6c1e4576a77f01e02973f67e962bb58d/state.go#L33-L69","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nfunc handler(c fiber.Ctx) error {\n    db := c.App().State().MustGet(\"database\") // panics if unregistered\n    ...\n}\n\n// after\nfunc handler(c fiber.Ctx) error {\n    dep, ok := c.App().State().Get(\"database\")\n    if !ok {\n        return fiber.NewError(fiber.StatusServiceUnavailable, \"database unavailable\")\n    }\n    db := dep.(*sql.DB)\n    ...\n}","handlingStrategy":"validation","validationCode":"func getDependencyOr(s *fiber.State, key string) (any, error) {\n    dep, ok := s.Get(key)\n    if !ok {\n        return nil, fmt.Errorf(\"dependency %q not registered\", key)\n    }\n    return dep, nil\n}\n// dep, err := getDependencyOr(app.State(), \"db\"); if err != nil { handle }","typeGuard":"func stateHasKey(s *fiber.State, key string) bool { _, ok := s.Get(key); return ok }","tryCatchPattern":null,"preventionTips":["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."],"tags":["state","panic","runtime","config"],"backgroundTag":null,"analyzedSha":"a105acad6c1e4576a77f01e02973f67e962bb58d","analyzedAt":"2026-08-11T17:33:26.942Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}