gofiber/fiber · error

route handler 'fn' cannot be nil

Error message

route handler 'fn' cannot be nil

What it means

Group.Route creates a sub-router under a prefix and invokes the supplied function to register routes on it. Passing nil is a programming error with nothing to execute, so it panics with "route handler 'fn' cannot be nil". This mirrors the same guard on domainRouter.Route.

Source

Thrown at group.go:243

		matcher: parseDomainPattern(host),
	}
}

// RouteChain creates a Registering instance scoped to the group's prefix,
// allowing chained route declarations for the same path.
func (grp *Group) RouteChain(path string) Register {
	// Create new group
	register := &Registering{app: grp.app, group: grp, path: getGroupPath(grp.Prefix, path)}

	return register
}

// Route is used to define routes with a common prefix inside the supplied
// function. It mirrors the legacy helper and reuses the Group method to create
// a sub-router.
func (grp *Group) Route(prefix string, fn func(router Router), name ...string) Router {
	if fn == nil {
		panic("route handler 'fn' cannot be nil")
	}
	// Create new group
	group := grp.Group(prefix)
	if len(name) > 0 {
		group.Name(name[0])
	}

	// Define routes
	fn(group)

	return group
}

View on GitHub (pinned to 9a4c7e57fe)

Solutions

  1. Pass a non-nil func(router Router) that registers the sub-routes.
  2. Guard optional route groups with an if instead of passing nil.
  3. Initialize registration variables at declaration so they cannot be nil at call time.

Example fix

// before
grp.Route("/api", nil)
// after
grp.Route("/api", func(r fiber.Router) {
    r.Get("/items", listItems)
})
Defensive patterns

Strategy: validation

Validate before calling

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

Prevention

When it happens

Trigger: Calling grp.Route("/api", nil), or passing a function variable that is nil because a registration function was looked up but not found.

Common situations: Splitting routes into files and forgetting to assign the registration function in one file. Conditionally registering routes and passing nil in the else branch. Refactoring that leaves a dangling nil.

Related errors


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