{"id":"4e40facef03fcb75","repo":"labstack/echo","slug":"failed-to-parse-value-err-w","errorCode":null,"errorMessage":"failed to parse value, err: %w","messagePattern":"failed to parse value, err: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"binder_generic.go","lineNumber":400,"sourceCode":"//   - uint\n//   - uint8/byte\n//   - uint16\n//   - uint32\n//   - uint64\n//   - string\n//   - echo.BindUnmarshaler interface\n//   - encoding.TextUnmarshaler interface\n//   - json.Unmarshaler interface\n//   - time.Duration\n//   - time.Time use echo.TimeOpts or echo.TimeLayout to set time parsing configuration\nfunc ParseValueOr[T any](value string, defaultValue T, opts ...any) (T, error) {\n\tif len(value) == 0 {\n\t\treturn defaultValue, nil\n\t}\n\tvar tmp T\n\tif err := bindValue(value, &tmp, opts...); err != nil {\n\t\tvar zero T\n\t\treturn zero, fmt.Errorf(\"failed to parse value, err: %w\", err)\n\t}\n\treturn tmp, nil\n}\n\nfunc bindValue(value string, dest any, opts ...any) error {\n\t// NOTE: if this function is ever made public the dest should be checked for nil\n\t// values when dealing with interfaces\n\tif len(opts) > 0 {\n\t\tif _, isTime := dest.(*time.Time); !isTime {\n\t\t\treturn fmt.Errorf(\"options are only supported for time.Time, got %T\", dest)\n\t\t}\n\t}\n\n\tswitch d := dest.(type) {\n\tcase *bool:\n\t\tn, err := strconv.ParseBool(value)\n\t\tif err != nil {\n\t\t\treturn err","sourceCodeStart":382,"sourceCodeEnd":418,"githubUrl":"https://github.com/labstack/echo/blob/05489dc1730161df26b72d1ae2a3ba6fb8178fc7/binder_generic.go#L382-L418","documentation":"Returned by echo.ParseValueOr (binder_generic.go:398-401) — and propagated from ParseValue — when bindValue fails to convert the string into type T. The wrapped error is the underlying strconv/time/Unmarshal failure identifying the real cause.","triggerScenarios":"ParseValue[int](\"abc\") (strconv.ParseInt fails); ParseValue[float64](\"1,5\") (comma decimal); ParseValue[time.Duration](\"5x\") (bad unit); a custom BindUnmarshaler returning an error.","commonSituations":"Untrusted query/form values that don't match the target scalar type; locale-specific number formats; bad duration units.","solutions":["Sanitise the string before parsing (trim, drop thousands separators, normalise decimal point)","Return a field-level 400 to the client naming the offending field","Implement BindUnmarshaler on a custom type to control parsing"],"exampleFix":"// before\nn, err := echo.ParseValue[int](\"12a\")\n// after\ns := strings.TrimSpace(\"12a\")\ns = strings.ReplaceAll(s, \",\", \"\")\nn, err := echo.ParseValue[int](s)","handlingStrategy":"try-catch","validationCode":"if _, err := strconv.ParseInt(strings.TrimSpace(value), 10, 64); err != nil {\n    return c.JSON(http.StatusBadRequest, map[string]string{\"page\": \"must be an integer\"})\n}","typeGuard":null,"tryCatchPattern":"v, err := echo.ParseValue[T](raw)\nif err != nil {\n    return c.JSON(http.StatusBadRequest, map[string]string{\"error\": err.Error()})\n}","preventionTips":["Sanitise numeric input before parsing","Return field-level 400 errors","Use BindUnmarshaler for domain types"],"tags":["binding","parse","validation"],"analyzedSha":"05489dc1730161df26b72d1ae2a3ba6fb8178fc7","analyzedAt":"2026-08-04T21:32:47.783Z","schemaVersion":2}