{"record":{"id":"430c4f8e5e417602","repo":"inancgumus/learngo","slug":"wrong-input-q-line-d-430c4f","errorCode":null,"errorMessage":"wrong input: %q (line #%d)","messagePattern":"wrong input: %q \\(line #(.+?)\\)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"logparser/v4/parser.go","lineNumber":58,"sourceCode":"\tif p.lerr != nil {\n\t\treturn\n\t}\n\n\tp.lines++\n\n\tfields := strings.Fields(line)\n\tif len(fields) != 2 {\n\t\tp.lerr = fmt.Errorf(\"wrong input: %v (line #%d)\", fields, p.lines)\n\t\treturn\n\t}\n\n\tvar err error\n\n\tr.domain = fields[0]\n\tr.visits, err = strconv.Atoi(fields[1])\n\n\tif r.visits < 0 || err != nil {\n\t\tp.lerr = fmt.Errorf(\"wrong input: %q (line #%d)\", fields[1], p.lines)\n\t}\n\treturn\n}\n\n// update updates all the parsing results using the given parsing result\nfunc update(p *parser, r result) {\n\tif p.lerr != nil {\n\t\treturn\n\t}\n\n\t// Collect the unique domains\n\tif _, ok := p.sum[r.domain]; !ok {\n\t\tp.domains = append(p.domains, r.domain)\n\t}\n\n\t// Keep track of total and per domain visits\n\tp.total += r.visits\n","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/inancgumus/learngo/blob/3c475a78e54336c6255e925ff66b8ef4da6a2ae7/logparser/v4/parser.go#L40-L76","documentation":"In v4's Parse, after fields = strings.Fields(line), the second field is converted with strconv.Atoi. If Atoi fails or the visits count is negative, Parse stores 'wrong input: %q (line #%d)' (quoting the visits token and line number) in p.lerr and returns; the result for that line is not passed to update.","triggerScenarios":"A line whose visits column is negative ('example.com -1') or unparseable ('example.com 12x') during v4 Parse.","commonSituations":"Negative deltas from upstream aggregation bugs, numbers with units or separators, typos, or locale-formatted numbers ('1.000').","solutions":["Fix the offending value in the input log","Pre-validate the visits token as a non-negative integer","Separate the 'not a number' and 'negative' cases for clearer diagnostics"],"exampleFix":"// before\nif r.visits < 0 || err != nil {\n    p.lerr = fmt.Errorf(\"wrong input: %q (line #%d)\", fields[1], p.lines)\n}\n// after\nif err != nil {\n    p.lerr = fmt.Errorf(\"line #%d: visits %q is not a number\", p.lines, fields[1])\n} else if r.visits < 0 {\n    p.lerr = fmt.Errorf(\"line #%d: visits %d is negative\", p.lines, r.visits)\n}","handlingStrategy":"validation","validationCode":"var countRe = regexp.MustCompile(`^\\d+$`)\nfs := strings.Fields(line)\nif len(fs) == 2 && !countRe.MatchString(fs[1]) {\n    continue // skip non-numeric visits before Parse\n}","typeGuard":"func isNonNegInt(s string) bool {\n    n, err := strconv.Atoi(s)\n    return err == nil && n >= 0\n}","tryCatchPattern":"if p.lerr != nil {\n    var ne *strconv.NumError\n    if errors.As(p.lerr, &ne) { /* not always wrapped; inspect message */ }\n    log.Fatalf(\"parse stopped: %v\", p.lerr)\n}","preventionTips":["Validate the visits token before feeding the line to Parse","Split negative vs. non-numeric handling for clearer errors","Check p.lerr after parsing and report the line number","Normalize locale-formatted numbers upstream"],"tags":["go","parsing","strconv","v4"],"backgroundTag":"invalid-integer-field","analyzedSha":"3c475a78e54336c6255e925ff66b8ef4da6a2ae7","analyzedAt":"2026-09-02T10:14:38.492Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T16:17:10.729Z"}