{"id":"6527bacf6250f375","repo":"gofiber/fiber","slug":"route-handler-fn-cannot-be-nil-6527ba","errorCode":null,"errorMessage":"route handler 'fn' cannot be nil","messagePattern":"route handler 'fn' cannot be nil","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"domain.go","lineNumber":594,"sourceCode":"\t\tapp:     d.app,\n\t\tgroup:   newGrp,\n\t\tmatcher: d.matcher,\n\t}\n}\n\n// RouteChain creates a Registering instance for the domain router.\nfunc (d *domainRouter) RouteChain(path string) Register {\n\treturn &domainRegistering{\n\t\tdomain: d,\n\t\tpath:   d.registerPath(path),\n\t}\n}\n\n// Route defines routes with a common prefix inside the supplied function,\n// scoped to the domain pattern.\nfunc (d *domainRouter) Route(prefix string, fn func(router Router), name ...string) Router {\n\tif fn == nil {\n\t\tpanic(\"route handler 'fn' cannot be nil\")\n\t}\n\n\tgroup := d.Group(prefix)\n\tif len(name) > 0 {\n\t\tgroup.Name(name[0])\n\t}\n\n\tfn(group)\n\n\treturn group\n}\n\n// Name assigns a name to the most recently registered route.\n// When the domain router was created from a Group, this delegates to the\n// group's Name method so that group name prefixes are applied correctly.\nfunc (d *domainRouter) Name(name string) Router {\n\tif d.group != nil {\n\t\td.group.Name(name)","sourceCodeStart":576,"sourceCodeEnd":612,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/domain.go#L576-L612","documentation":"domainRouter.Route defines a group of routes under a domain-scoped prefix by invoking the supplied function with the sub-router. Passing a nil function is a programming error (there is nothing to register), so it panics with \"route handler 'fn' cannot be nil\".","triggerScenarios":"Calling domainRouter.Route(\"/api\", nil), or passing a function variable that was never assigned (e.g. loaded from a registry that returned nil for a missing entry).","commonSituations":"Conditionally building routes and passing nil in the disabled branch. Refactoring route registration into variables and leaving one uninitialized. Copying a Route call and deleting the body but keeping the call.","solutions":["Pass a non-nil func(router Router), e.g. domainRouter.Route(\"/api\", func(r fiber.Router) { r.Get(\"/users\", ... ) }).","If the routes are optional, guard the Route call with an if rather than passing nil.","Initialize route-registration variables at declaration so a nil never reaches Route."],"exampleFix":"// before\ndomain.Route(\"/api\", nil)\n// after\ndomain.Route(\"/api\", func(r fiber.Router) {\n    r.Get(\"/users\", getUsers)\n})","handlingStrategy":"validation","validationCode":"if fn == nil {\n    log.Fatal(\"domain: Route requires a non-nil registration function\")\n}\ndomain.Route(\"/api\", fn)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass a literal func literal to Route rather than a variable that may be nil.","Guard optional route groups with an if instead of passing nil."],"tags":["routing","domain","handler","panic"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}