{"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":"critical","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/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/state.go#L33-L69","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before — panics if \"db\" was never set\nconn := app.State().MustGet(\"db\").(*sql.DB)\n\n// after — handle absence explicitly\nv, ok := app.State().Get(\"db\")\nif !ok {\n\treturn fmt.Errorf(\"dependency %q not registered\", \"db\")\n}\nconn := v.(*sql.DB)","handlingStrategy":"validation","validationCode":"const depKey = \"db\" // shared with the Set call site\nif !app.State().Has(depKey) {\n\treturn fmt.Errorf(\"dependency %q not registered\", depKey)\n}\nv := app.State().MustGet(depKey)","typeGuard":null,"tryCatchPattern":"defer func() {\n\tif r := recover(); r != nil {\n\t\tif msg, _ := r.(string); msg == \"state: dependency not found!\" {\n\t\t\t// handle missing dependency without crashing the request\n\t\t\treturn\n\t\t}\n\t\tpanic(r) // re-raise unrelated panics\n\t}\n}()\nv := app.State().MustGet(\"db\")","preventionTips":["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."],"tags":["state","dependency-injection","panic","key-not-found"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}