{"record":{"id":"bc8dc5f17950504f","repo":"inancgumus/learngo","slug":"wrong-input-q-line-d-bc8dc5","errorCode":null,"errorMessage":"wrong input: %q (line #%d)","messagePattern":"wrong input: %q \\(line #(.+?)\\)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"logparser/v3/parser.go","lineNumber":49,"sourceCode":"\n// newParser constructs, initializes and returns a new parser\nfunc newParser() parser {\n\treturn parser{sum: make(map[string]result)}\n}\n\n// parse parses a log line and returns the parsed result with an error\nfunc parse(p parser, line string) (parsed result, err error) {\n\tfields := strings.Fields(line)\n\tif len(fields) != 2 {\n\t\terr = fmt.Errorf(\"wrong input: %v (line #%d)\", fields, p.lines)\n\t\treturn\n\t}\n\n\tparsed.domain = fields[0]\n\n\tparsed.visits, err = strconv.Atoi(fields[1])\n\tif parsed.visits < 0 || err != nil {\n\t\terr = fmt.Errorf(\"wrong input: %q (line #%d)\", fields[1], p.lines)\n\t\treturn\n\t}\n\n\treturn\n}\n\n// update updates the parser for the given parsing result\nfunc update(p parser, parsed result) parser {\n\tdomain, visits := parsed.domain, parsed.visits\n\n\t// Collect the unique domains\n\tif _, ok := p.sum[domain]; !ok {\n\t\tp.domains = append(p.domains, domain)\n\t}\n\n\t// Keep track of total and per domain visits\n\tp.total += visits\n","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/inancgumus/learngo/blob/3c475a78e54336c6255e925ff66b8ef4da6a2ae7/logparser/v3/parser.go#L31-L67","documentation":"After splitting, v3's parse converts the second field with strconv.Atoi. If conversion fails or the visits value is negative, it replaces the error with 'wrong input: %q (line #%d)' quoting just the offending visits token plus the line number, and returns without a result.","triggerScenarios":"A line like 'example.com -3' (negative) or 'example.com abc' (non-numeric) passed to parse; Atoi's error or the < 0 check triggers the replacement error.","commonSituations":"Signed counts from buggy upstream writers, typos ('l0' instead of '10'), values with separators ('1,000'), or placeholder values ('N/A') in exported logs.","solutions":["Fix the negative/non-numeric visits value in the input","Pre-validate with strconv.Atoi or a ^\\d+$ regex before parse","Decide whether negative counts should be rejected or clamped/warned"],"exampleFix":"// before\nif parsed.visits < 0 || err != nil {\n    err = fmt.Errorf(\"wrong input: %q (line #%d)\", fields[1], p.lines)\n    return\n}\n// after\nif err != nil {\n    err = fmt.Errorf(\"line #%d: visits %q is not a number\", p.lines, fields[1])\n    return\n}\nif parsed.visits < 0 {\n    err = fmt.Errorf(\"line #%d: visits %d is negative\", p.lines, parsed.visits)\n    return\n}","handlingStrategy":"validation","validationCode":"var visitsRe = regexp.MustCompile(`^\\d+$`)\nfs := strings.Fields(line)\nif len(fs) == 2 && !visitsRe.MatchString(fs[1]) {\n    log.Printf(\"line invalid, skipping\")\n    continue\n}","typeGuard":"func isValidVisits(s string) bool {\n    n, err := strconv.Atoi(s)\n    return err == nil && n >= 0\n}","tryCatchPattern":"parsed, err := parse(p, line)\nif err != nil {\n    var ve *strconv.NumError\n    if errors.As(err, &ve) {\n        log.Printf(\"non-numeric visits on line %d: %v\", p.lines, ve.Num)\n        continue\n    }\n    return err\n}","preventionTips":["Pre-validate visits with ^\\d+$ or strconv.Atoi before parse","Decide policy for negative counts (reject vs. warn)","Quote the offending token and line number in messages","Cover negative and non-numeric cases in unit tests"],"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"}