{"record":{"id":"39400719be0b32a6","repo":"inancgumus/learngo","slug":"invalid-number","errorCode":null,"errorMessage":"invalid number","messagePattern":"invalid number","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"logparser/functional/field.go","lineNumber":36,"sourceCode":"type field struct{ err error }\n\n// uatoi parses an unsigned integer string and saves the error.\n// it assumes that the val is unsigned.\n// for ease of usability: it returns an int instead of uint.\nfunc (f *field) uatoi(name, val string) int {\n\tn, err := strconv.Atoi(val)\n\tif err != nil || n < 0 {\n\t\tf.err = fmt.Errorf(\"incorrect field -> %q = %q\", name, val)\n\t}\n\treturn n\n}\n\nfunc atoi(input []byte) (int, error) {\n\tval := 0\n\tfor i := 0; i < len(input); i++ {\n\t\tchar := input[i]\n\t\tif char < '0' || char > '9' {\n\t\t\treturn 0, errors.New(\"invalid number\")\n\t\t}\n\t\tval = val*10 + int(char) - '0'\n\t}\n\treturn val, nil\n}\n","sourceCodeStart":18,"sourceCodeEnd":42,"githubUrl":"https://github.com/inancgumus/learngo/blob/3c475a78e54336c6255e925ff66b8ef4da6a2ae7/logparser/functional/field.go#L18-L42","documentation":"atoi() is a hand-rolled byte-to-int parser used by fastParseFields; it only accepts ASCII digits 0-9. Any non-digit byte in the input causes it to abandon parsing and return \"invalid number\" with a zero value.","triggerScenarios":"fastParseFields passes a field byte-slice containing any character outside '0'-'9' to atoi — e.g. a log field like \"12.5\", \"-3\", \"1e2\", \"0x10\", \"12a\", or an empty/non-numeric token.","commonSituations":"Parsing log lines where the expected numeric column holds N/A, floats, signed numbers, or misaligned columns due to a changed log format or separator.","solutions":["Fix or normalize the log format so the parsed field contains only digits.","Pre-validate the field with a digit-only check before calling atoi.","Replace atoi with strconv.Atoi (or ParseFloat) if negatives/floats must be supported.","Skip or quarantine malformed records instead of failing the whole parse."],"exampleFix":"// before\nval = val*10 + int(char) - '0'\n// after (support negatives/robustness)\nreturn strconv.Atoi(string(input))","handlingStrategy":"validation","validationCode":"func isDigits(b []byte) bool {\n    if len(b) == 0 { return false }\n    for _, c := range b {\n        if c < '0' || c > '9' { return false }\n    }\n    return true\n}\nif !isDigits(field) { skipRecord() }","typeGuard":"func isNumericField(b []byte) bool {\n    for _, c := range b {\n        if c < '0' || c > '9' { return false }\n    }\n    return len(b) > 0\n}","tryCatchPattern":"n, err := atoi(field)\nif err != nil {\n    if err.Error() == \"invalid number\" {\n        log.Printf(\"skipping non-numeric field %q\", field)\n        return 0, nil // or propagate\n    }\n    return 0, err\n}","preventionTips":["Validate the log format with a schema or golden-file test before parsing","Trim and check each column before numeric conversion","Prefer strconv.Atoi for inputs that may include signs or leading whitespace","Quarantine malformed lines instead of aborting the whole parse"],"tags":["parsing","validation","logparser"],"backgroundTag":"invalid-number-format","analyzedSha":"3c475a78e54336c6255e925ff66b8ef4da6a2ae7","analyzedAt":"2026-09-02T10:14:38.492Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T16:17:10.729Z"}