gofiber/fiber · error

route handler 'fn' cannot be nil

Error message

route handler 'fn' cannot be nil

What it means

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".

Source

Thrown at domain.go:594

		app:     d.app,
		group:   newGrp,
		matcher: d.matcher,
	}
}

// RouteChain creates a Registering instance for the domain router.
func (d *domainRouter) RouteChain(path string) Register {
	return &domainRegistering{
		domain: d,
		path:   d.registerPath(path),
	}
}

// Route defines routes with a common prefix inside the supplied function,
// scoped to the domain pattern.
func (d *domainRouter) Route(prefix string, fn func(router Router), name ...string) Router {
	if fn == nil {
		panic("route handler 'fn' cannot be nil")
	}

	group := d.Group(prefix)
	if len(name) > 0 {
		group.Name(name[0])
	}

	fn(group)

	return group
}

// Name assigns a name to the most recently registered route.
// When the domain router was created from a Group, this delegates to the
// group's Name method so that group name prefixes are applied correctly.
func (d *domainRouter) Name(name string) Router {
	if d.group != nil {
		d.group.Name(name)

View on GitHub (pinned to 9a4c7e57fe)

Solutions

  1. Pass a non-nil func(router Router), e.g. domainRouter.Route("/api", func(r fiber.Router) { r.Get("/users", ... ) }).
  2. If the routes are optional, guard the Route call with an if rather than passing nil.
  3. Initialize route-registration variables at declaration so a nil never reaches Route.

Example fix

// before
domain.Route("/api", nil)
// after
domain.Route("/api", func(r fiber.Router) {
    r.Get("/users", getUsers)
})
Defensive patterns

Strategy: validation

Validate before calling

if fn == nil {
    log.Fatal("domain: Route requires a non-nil registration function")
}
domain.Route("/api", fn)

Prevention

When it happens

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

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

Related errors


AI-assisted analysis of gofiber/fiber@9a4c7e57fe (2026-08-04). Data as JSON: /data/errors/6527bacf6250f375.json. Report an issue: GitHub.