{"record":{"id":"2278599ccaea2171","repo":"inancgumus/learngo","slug":"incorrect-s-q","errorCode":null,"errorMessage":"incorrect %s: %q","messagePattern":"incorrect (.+?): %q","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"logparser/testing/report/result.go","lineNumber":58,"sourceCode":"\tfields := strings.Fields(line)\n\tif len(fields) != 3 {\n\t\treturn r, fmt.Errorf(\"missing fields: %v\", fields)\n\t}\n\n\tf := new(field)\n\tr.Domain = fields[0]\n\tr.Visits = f.atoi(\"visits\", fields[1])\n\tr.TimeSpent = f.atoi(\"time spent\", fields[2])\n\treturn r, f.err\n}\n\n// field helps for field parsing\ntype field struct{ err error }\n\nfunc (f *field) atoi(name, val string) int {\n\tn, err := strconv.Atoi(val)\n\tif n < 0 || err != nil {\n\t\tf.err = fmt.Errorf(\"incorrect %s: %q\", name, val)\n\t}\n\treturn n\n}\n","sourceCodeStart":40,"sourceCodeEnd":62,"githubUrl":"https://github.com/inancgumus/learngo/blob/3c475a78e54336c6255e925ff66b8ef4da6a2ae7/logparser/testing/report/result.go#L40-L62","documentation":"field.atoi converts a numeric field with strconv.Atoi and rejects values that are negative or non-numeric, storing the error on the field struct as 'incorrect %s: %q' where %s is the field name ('visits' or 'time spent') and %q the offending value. The caller is expected to check f.err after parsing all fields.","triggerScenarios":"A log line whose visits or time-spent column is negative (-5) or not a plain integer (e.g. 'abc', '1.5', '12,000', '0x10').","commonSituations":"Units accidentally included ('30s', '45min'), thousands separators, signed values from upstream systems, float durations, or corrupted log data.","solutions":["Correct the offending value in the log line","Pre-validate numeric fields with a regex like ^\\d+$ before atoi","Strip units/separators (e.g. remove commas, trailing time units) before conversion","Check f.err immediately after parse so the right field/value is reported to the user"],"exampleFix":"// before\nn, err := strconv.Atoi(val)\nif n < 0 || err != nil {\n    f.err = fmt.Errorf(\"incorrect %s: %q\", name, val)\n}\n// after\nn, err := strconv.Atoi(strings.TrimSuffix(val, \"s\"))\nif err != nil || n < 0 {\n    f.err = fmt.Errorf(\"incorrect %s: %q (must be a non-negative integer)\", name, val)\n}","handlingStrategy":"validation","validationCode":"var numRe = regexp.MustCompile(`^\\d+$`)\nfunc isNonNegativeInt(s string) bool { return numRe.MatchString(s) }\n// check fields[1] and fields[2] with isNonNegativeInt before parse","typeGuard":"func isCount(s string) bool {\n    n, err := strconv.Atoi(s)\n    return err == nil && n >= 0\n}","tryCatchPattern":"r, err := parse(line)\nif err != nil {\n    if f.err != nil { // field.atoi stored the failure\n        log.Printf(\"bad numeric field in %q: %v\", line, f.err)\n        continue\n    }\n    return err\n}","preventionTips":["Strip units and thousands separators before Atoi","Use ^\\d+$ regex pre-validation on numeric columns","Reject negatives explicitly with a dedicated message","Check f.err immediately after parse to attribute the failure to the right field"],"tags":["go","parsing","strconv"],"backgroundTag":"invalid-integer-field","analyzedSha":"3c475a78e54336c6255e925ff66b8ef4da6a2ae7","analyzedAt":"2026-09-02T10:14:38.492Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T16:17:10.729Z"}