labstack/echo · error

context SetPathValues called with nil PathValues

Error message

context SetPathValues called with nil PathValues

What it means

Panicked by Context.SetPathValues (context.go:306) when called with a nil PathValues. PathValues is a slice type; a nil slice is treated as a programmer error because the router relies on a non-nil, indexable slice for parameter access. This guard prevents nil-pointer/index panics deeper in routing.

Source

Thrown at context.go:306

// Notes for DefaultRouter implementation:
// Path parameter could be empty for cases like that:
// * route `/release-:version/bin` and request URL is `/release-/bin`
// * route `/api/:version/image.jpg` and request URL is `/api//image.jpg`
// but not when path parameter is last part of route path
// * route `/download/file.:ext` will not match request `/download/file.`
func (c *Context) ParamOr(name, defaultValue string) string {
	return c.pathValues.GetOr(name, defaultValue)
}

// PathValues returns path parameter values.
func (c *Context) PathValues() PathValues {
	return *c.pathValues
}

// SetPathValues sets path parameters for current request.
func (c *Context) SetPathValues(pathValues PathValues) {
	if pathValues == nil {
		panic("context SetPathValues called with nil PathValues")
	}
	c.setPathValues(&pathValues)
}

// InitializeRoute sets the route related variables of this request to the context.
func (c *Context) InitializeRoute(ri *RouteInfo, pathValues *PathValues) {
	c.route = ri
	c.path = ri.Path
	c.setPathValues(pathValues)
}

func (c *Context) setPathValues(pv *PathValues) {
	// Router accesses c.pathValues by index and may resize it to full capacity during routing
	// for that to work without going out-of-bounds we must make sure that c.pathValues slice is not replaced with smaller
	// slice than Router can set when routing Route with maximum amount of parameters.
	pathValues := c.pathValues
	if cap(*c.pathValues) < len(*pv) {
		// normally we should not end up here. pathValues is normally sized to Echo.contextPathParamAllocSize which should not

View on GitHub (pinned to 05489dc173)

Solutions

  1. Pass a non-nil PathValues, e.g. echo.PathValues{} (empty but initialized) when there are no params.
  2. Initialize the slice literal: c.SetPathValues(echo.PathValues{{Name: "id", Value: "42"}}).
  3. In custom routers, allocate PathValues before calling SetPathValues.

Example fix

// before
c.SetPathValues(nil) // panics
// after
c.SetPathValues(echo.PathValues{}) // empty but non-nil
Defensive patterns

Strategy: validation

Validate before calling

// Guard custom router code that rebinds path values.
func setPathValuesSafe(c echo.Context, pv echo.PathValues) {
    if pv == nil {
        pv = echo.PathValues{}
    }
    c.SetPathValues(pv)
}

Prevention

When it happens

Trigger: Calling c.SetPathValues(nil) or passing an uninitialized PathValues variable in a custom router, test helper, or re-binding logic. Echo's own router always provides a non-nil slice via InitializeRoute.

Common situations: Writing a custom router that re-binds path values; test code that forgets to initialize the PathValues slice; refactoring that passes a nil where a non-nil slice is expected.

Related errors


AI-assisted analysis of labstack/echo@05489dc173 (2026-08-04). Data as JSON: /data/errors/e6178cf2770fb4cc.json. Report an issue: GitHub.