{"record":{"id":"20873288e19964f4","repo":"gofiber/fiber","slug":"fiber-service-at-index-d-is-nil","errorCode":null,"errorMessage":"fiber: service at index %d is nil","messagePattern":"fiber: service at index (.+?) is nil","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"services.go","lineNumber":40,"sourceCode":"\tState(ctx context.Context) (string, error)\n\n\t// Terminate terminates the service, returning an error if it fails.\n\tTerminate(ctx context.Context) error\n}\n\n// hasConfiguredServices Checks if there are any services for the current application.\nfunc (app *App) hasConfiguredServices() bool {\n\treturn len(app.configured.Services) > 0\n}\n\nfunc (app *App) validateConfiguredServices() error {\n\treturn validateServicesSlice(app.configured.Services)\n}\n\nfunc validateServicesSlice(services []Service) error {\n\tfor idx, srv := range services {\n\t\tif srv == nil {\n\t\t\treturn fmt.Errorf(\"fiber: service at index %d is nil\", idx)\n\t\t}\n\t}\n\treturn nil\n}\n\n// initServices If the app is configured to use services, this function registers\n// a post shutdown hook to shutdown them after the server is closed.\n// This function panics if there is an error starting the services.\nfunc (app *App) initServices() {\n\tif !app.hasConfiguredServices() {\n\t\treturn\n\t}\n\n\tif err := app.startServices(app.servicesStartupCtx()); err != nil {\n\t\tpanic(err)\n\t}\n}\n","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/gofiber/fiber/blob/a105acad6c1e4576a77f01e02973f67e962bb58d/services.go#L22-L58","documentation":"Returned by validateConfiguredServices() (via validateServicesSlice) when any element of fiber.Config.Services is nil. This runs during App startup validation, before the server begins listening. Fiber refuses to start a service slice with gaps because the Start/Terminate loop would dereference nil.","triggerScenarios":"Configuring app with fiber.New(fiber.Config{Services: []fiber.Service{someSvc, nil}}) — a nil slipped into the slice via a conditional append (e.g., `Services: []fiber.Service{a, maybeNil}` where maybeNil was nil).","commonSituations":"Building the Services slice conditionally and forgetting to filter nils (e.g., `var db fiber.Service; if enabled { db = ... }; Services: []fiber.Service{db}`); a service factory returning nil on a feature flag; refactoring that removed an initializer but left the slice slot.","solutions":["Filter nils out of the slice before passing it to Config: build with append only when non-nil.","Use a constructor that always returns a non-nil Service or returns an error.","Add a unit test asserting every element of cfg.Services is non-nil."],"exampleFix":"// before — conditional nil in slice\nvar metrics fiber.Service\nif cfg.Metrics {\n    metrics = metricsSvc\n}\napp := fiber.New(fiber.Config{Services: []fiber.Service{db, metrics}})\n// -> fiber: service at index 1 is nil\n\n// after — append only non-nil\nsvcs := []fiber.Service{db}\nif cfg.Metrics {\n    svcs = append(svcs, metricsSvc)\n}\napp := fiber.New(fiber.Config{Services: svcs})","handlingStrategy":"validation","validationCode":"// Build the Services slice defensively, skipping nils.\nfunc buildServices(flags map[string]bool, mk map[string]func() fiber.Service) []fiber.Service {\n    out := make([]fiber.Service, 0, len(flags))\n    for name, on := range flags {\n        if !on { continue }\n        if s := mk[name](); s != nil { out = append(out, s) }\n    }\n    return out\n}","typeGuard":"func allServicesNonNil(s []fiber.Service) bool {\n    for _, s := range s { if s == nil { return false } }\n    return true\n}","tryCatchPattern":null,"preventionTips":["Build the Services slice with conditional append, never positional nils.","Assert in a unit test that every element is non-nil before fiber.New().","Make service factories return (Service, error), never nil."],"tags":["services","config","startup","nil-guard"],"backgroundTag":null,"analyzedSha":"a105acad6c1e4576a77f01e02973f67e962bb58d","analyzedAt":"2026-08-11T17:33:26.942Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}