{"record":{"id":"eee5b09cc1cf4bf7","repo":"gofiber/fiber","slug":"state-service-not-found","errorCode":null,"errorMessage":"state: service not found!","messagePattern":"state: service not found!","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"state.go","lineNumber":315,"sourceCode":"\t\t}\n\t\treturn true\n\t})\n\n\treturn length\n}\n\n// GetService returns a service present in the application's State.\nfunc GetService[T Service](s *State, key string) (T, bool) {\n\tsrv, ok := GetState[T](s, s.serviceKey(key))\n\treturn srv, ok\n}\n\n// MustGetService returns a service present in the application's State.\n// It panics if the service is not found.\nfunc MustGetService[T Service](s *State, key string) T {\n\tsrv, ok := GetService[T](s, key)\n\tif !ok {\n\t\tpanic(\"state: service not found!\")\n\t}\n\n\treturn srv\n}\n","sourceCodeStart":297,"sourceCodeEnd":320,"githubUrl":"https://github.com/gofiber/fiber/blob/a105acad6c1e4576a77f01e02973f67e962bb58d/state.go#L297-L320","documentation":"MustGetService retrieves a registered Service (a type implementing the Service interface) from the application State, keyed under a hashed internal prefix to keep service keys separate from ordinary dependencies. It panics if no service is registered under that key. Services are registered via the dedicated service-registration API, so a panic here means the expected service was never registered (or was registered under a different key) before the lookup.","triggerScenarios":"fiber.MustGetService[Cache](app.State(), \"redis\") before registering the redis service; a service registered on a sub-app's State but looked up on the parent; a key mismatch between registration and lookup.","commonSituations":"Service registration order issues (lookup during init before registration); a service keyed by a name that changed; tests that construct an App without the full service bootstrap; accessing a service from a mounted sub-app that has its own State.","solutions":["Use the non-panicking fiber.GetService[T] and handle the (service, ok) pair explicitly when absence is possible.","Register all services during app construction, before mounting handlers that look them up.","Keep service keys as package-level constants shared between registration and lookup sites."],"exampleFix":"// before\nsvc := fiber.MustGetService[Cache](app.State(), \"cache\") // panics if unregistered\n\n// after\nsvc, ok := fiber.GetService[Cache](app.State(), \"cache\")\nif !ok {\n    return fiber.NewError(fiber.StatusServiceUnavailable, \"cache service unavailable\")\n}","handlingStrategy":"validation","validationCode":"func getServiceOrErr[T fiber.Service](s *fiber.State, key string) (T, error) {\n    var zero T\n    srv, ok := fiber.GetService[T](s, key)\n    if !ok { return zero, fmt.Errorf(\"service %q not registered\", key) }\n    return srv, nil\n}","typeGuard":"func serviceIsRegistered[T fiber.Service](s *fiber.State, key string) bool {\n    _, ok := fiber.GetService[T](s, key)\n    return ok\n}","tryCatchPattern":null,"preventionTips":["Use fiber.GetService[T] over MustGetService when the service may legitimately be absent (e.g. optional features).","Register services in a dedicated bootstrap step before any handler that looks them up is mounted.","Share service-key constants between registration and lookup to avoid name drift."],"tags":["state","panic","runtime","services"],"backgroundTag":null,"analyzedSha":"a105acad6c1e4576a77f01e02973f67e962bb58d","analyzedAt":"2026-08-11T17:33:26.942Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}