gofiber/fiber · error

state: service not found!

Error message

state: service not found!

What it means

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.

Solutions

  1. Use the non-panicking fiber.GetService[T] and handle the (service, ok) pair explicitly when absence is possible.
  2. Register all services during app construction, before mounting handlers that look them up.
  3. Keep service keys as package-level constants shared between registration and lookup sites.

Example fix

// before
svc := fiber.MustGetService[Cache](app.State(), "cache") // panics if unregistered

// after
svc, ok := fiber.GetService[Cache](app.State(), "cache")
if !ok {
    return fiber.NewError(fiber.StatusServiceUnavailable, "cache service unavailable")
}
Defensive patterns

Strategy: validation

Validate before calling

func getServiceOrErr[T fiber.Service](s *fiber.State, key string) (T, error) {
    var zero T
    srv, ok := fiber.GetService[T](s, key)
    if !ok { return zero, fmt.Errorf("service %q not registered", key) }
    return srv, nil
}

Type guard

func serviceIsRegistered[T fiber.Service](s *fiber.State, key string) bool {
    _, ok := fiber.GetService[T](s, key)
    return ok
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of gofiber/fiber@a105acad6c (2026-08-11). Data as JSON: /api/errors/eee5b09cc1cf4bf7. Report an issue: GitHub.

Appendix: source

Thrown at state.go:315

		}
		return true
	})

	return length
}

// GetService returns a service present in the application's State.
func GetService[T Service](s *State, key string) (T, bool) {
	srv, ok := GetState[T](s, s.serviceKey(key))
	return srv, ok
}

// MustGetService returns a service present in the application's State.
// It panics if the service is not found.
func MustGetService[T Service](s *State, key string) T {
	srv, ok := GetService[T](s, key)
	if !ok {
		panic("state: service not found!")
	}

	return srv
}

View on GitHub (pinned to a105acad6c)