{"id":"e6178cf2770fb4cc","repo":"labstack/echo","slug":"context-setpathvalues-called-with-nil-pathvalues","errorCode":null,"errorMessage":"context SetPathValues called with nil PathValues","messagePattern":"context SetPathValues called with nil PathValues","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"context.go","lineNumber":306,"sourceCode":"// Notes for DefaultRouter implementation:\n// Path parameter could be empty for cases like that:\n// * route `/release-:version/bin` and request URL is `/release-/bin`\n// * route `/api/:version/image.jpg` and request URL is `/api//image.jpg`\n// but not when path parameter is last part of route path\n// * route `/download/file.:ext` will not match request `/download/file.`\nfunc (c *Context) ParamOr(name, defaultValue string) string {\n\treturn c.pathValues.GetOr(name, defaultValue)\n}\n\n// PathValues returns path parameter values.\nfunc (c *Context) PathValues() PathValues {\n\treturn *c.pathValues\n}\n\n// SetPathValues sets path parameters for current request.\nfunc (c *Context) SetPathValues(pathValues PathValues) {\n\tif pathValues == nil {\n\t\tpanic(\"context SetPathValues called with nil PathValues\")\n\t}\n\tc.setPathValues(&pathValues)\n}\n\n// InitializeRoute sets the route related variables of this request to the context.\nfunc (c *Context) InitializeRoute(ri *RouteInfo, pathValues *PathValues) {\n\tc.route = ri\n\tc.path = ri.Path\n\tc.setPathValues(pathValues)\n}\n\nfunc (c *Context) setPathValues(pv *PathValues) {\n\t// Router accesses c.pathValues by index and may resize it to full capacity during routing\n\t// for that to work without going out-of-bounds we must make sure that c.pathValues slice is not replaced with smaller\n\t// slice than Router can set when routing Route with maximum amount of parameters.\n\tpathValues := c.pathValues\n\tif cap(*c.pathValues) < len(*pv) {\n\t\t// normally we should not end up here. pathValues is normally sized to Echo.contextPathParamAllocSize which should not","sourceCodeStart":288,"sourceCodeEnd":324,"githubUrl":"https://github.com/labstack/echo/blob/05489dc1730161df26b72d1ae2a3ba6fb8178fc7/context.go#L288-L324","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass a non-nil PathValues, e.g. echo.PathValues{} (empty but initialized) when there are no params.","Initialize the slice literal: c.SetPathValues(echo.PathValues{{Name: \"id\", Value: \"42\"}}).","In custom routers, allocate PathValues before calling SetPathValues."],"exampleFix":"// before\nc.SetPathValues(nil) // panics\n// after\nc.SetPathValues(echo.PathValues{}) // empty but non-nil","handlingStrategy":"validation","validationCode":"// Guard custom router code that rebinds path values.\nfunc setPathValuesSafe(c echo.Context, pv echo.PathValues) {\n    if pv == nil {\n        pv = echo.PathValues{}\n    }\n    c.SetPathValues(pv)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass a non-nil PathValues (use echo.PathValues{} for empty).","In custom routers/tests, initialize the slice literal before SetPathValues.","Treat nil as a programming bug and fix at the call site."],"tags":["context","router","panic","programming-error"],"analyzedSha":"05489dc1730161df26b72d1ae2a3ba6fb8178fc7","analyzedAt":"2026-08-04T21:32:47.783Z","schemaVersion":2}