{"id":"c8b32ddffb2b4f8c","repo":"gofiber/fiber","slug":"failed-to-convert-w","errorCode":null,"errorMessage":"failed to convert: %w","messagePattern":"failed to convert: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"helpers.go","lineNumber":1294,"sourceCode":"\t}\n\n\tswitch m {\n\tcase MethodPut, MethodDelete:\n\t\treturn true\n\tdefault:\n\t\treturn false\n\t}\n}\n\n// Convert a string value to a specified type, handling errors and optional default values.\nfunc Convert[T any](value string, converter func(string) (T, error), defaultValue ...T) (T, error) {\n\tconverted, err := converter(value)\n\tif err != nil {\n\t\tif len(defaultValue) > 0 {\n\t\t\treturn defaultValue[0], nil\n\t\t}\n\n\t\treturn converted, fmt.Errorf(\"failed to convert: %w\", err)\n\t}\n\n\treturn converted, nil\n}\n\nvar (\n\terrParsedEmptyString = errors.New(\"parsed result is empty string\")\n\terrParsedEmptyBytes  = errors.New(\"parsed result is empty bytes\")\n\terrParsedType        = errors.New(\"unsupported generic type\")\n\t// errParseValue flags a failed numeric/bool parse; callers only test err != nil.\n\terrParseValue = errors.New(\"failed to parse value\")\n)\n\n// genericParseType parses str into V. Parse failures return the static errParseValue\n// sentinel: the error is never surfaced (callers only test err != nil), so a flat\n// sentinel is enough and avoids a per-call fmt.Errorf alloc on the hot default path.\nfunc genericParseType[V GenericType](str string) (V, error) {\n\tvar v V","sourceCodeStart":1276,"sourceCodeEnd":1312,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/helpers.go#L1276-L1312","documentation":"Returned by the generic fiber.Convert[T] helper when the supplied converter function fails AND no default value was provided. Convert wraps any converter (strconv.Atoi, time.Parse, etc.) so callers get a uniform error. If a default value is passed, Convert returns it instead of erroring.","triggerScenarios":"Calling fiber.Convert(value, strconv.Atoi) where 'value' is non-numeric and no default is given, e.g. fiber.Convert(\"abc\", strconv.Atoi). The converter returns an error which Convert re-wraps as 'failed to convert: %w'.","commonSituations":"Parsing query params or form fields without prior validation, a missing default for optional numeric params, or a format mismatch (e.g. time.Parse with the wrong layout).","solutions":["Pass a default value as the variadic arg: fiber.Convert(value, strconv.Atoi, 0).","Validate the input string format before calling Convert.","Inspect the wrapped error to give a precise error message to the caller.","Use errors.Is/As on the wrapped error to branch on the underlying parse failure."],"exampleFix":"// before\nn, err := fiber.Convert(c.Query(\"page\"), strconv.Atoi)\n\n// after\nn, err := fiber.Convert(c.Query(\"page\"), strconv.Atoi, 1)\n// or validate first:\n// if !regexp.MustCompile(`^\\d+$`).MatchString(c.Query(\"page\")) { ... }","handlingStrategy":"fallback","validationCode":"// Guard conversion with a default so it never errors\nn, _ := fiber.Convert(c.Query(\"page\"), strconv.Atoi, 1)","typeGuard":null,"tryCatchPattern":"n, err := fiber.Convert(c.Query(\"page\"), strconv.Atoi)\nif err != nil {\n    return c.Status(fiber.StatusBadRequest).SendString(\"invalid page\")\n}","preventionTips":["Always pass a default value for optional conversions.","Validate input format before converting.","Inspect the wrapped error for a precise message."],"tags":["parsing","conversion","validation","go"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}