{"record":{"id":"ca5edf4d23667895","repo":"inancgumus/learngo","slug":"wrong-input-v-line-d-ca5edf","errorCode":null,"errorMessage":"wrong input: %v (line #%d)","messagePattern":"wrong input: (.+?) \\(line #(.+?)\\)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"logparser/v4/parser.go","lineNumber":48,"sourceCode":"\tlerr    error             // the last error occurred\n}\n\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) (r result) {\n\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","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/inancgumus/learngo/blob/3c475a78e54336c6255e925ff66b8ef4da6a2ae7/logparser/v4/parser.go#L30-L66","documentation":"The v4 parser is a stateful type: Parse increments p.lines, splits each line, and requires exactly 2 fields. On a mismatch it stores 'wrong input: %v (line #%d)' in p.lerr (instead of returning an error) and returns early; the caller checks p.lerr after the loop.","triggerScenarios":"Calling Parse on a line that doesn't split into exactly 'domain visits' — wrong field count including empty lines.","commonSituations":"Files mixing formats, blank separator lines between sections, legacy 3-field lines, or trailing blank line at EOF treated as a record.","solutions":["Skip blank/whitespace-only lines inside Parse before counting them","Validate the file format before parsing (e.g. check the first few lines)","Check p.lerr after parsing and report it with context"],"exampleFix":"// before\np.lines++\nfields := strings.Fields(line)\nif len(fields) != 2 {\n    p.lerr = fmt.Errorf(\"wrong input: %v (line #%d)\", fields, p.lines)\n    return\n}\n// after\nif strings.TrimSpace(line) == \"\" {\n    return // ignore blank lines\n}\np.lines++\nfields := strings.Fields(line)\nif len(fields) != 2 {\n    p.lerr = fmt.Errorf(\"wrong input on line #%d: %q\", p.lines, line)\n    return\n}","handlingStrategy":"validation","validationCode":"func validV4Line(line string) bool {\n    if strings.TrimSpace(line) == \"\" { return false }\n    return len(strings.Fields(line)) == 2\n}\n// only call p.Parse(line) when validV4Line(line)","typeGuard":"func isParsable(line string) bool {\n    fs := strings.Fields(line)\n    return len(fs) == 2 && func() bool { _, e := strconv.Atoi(fs[1]); return e == nil }()\n}","tryCatchPattern":"if err := p.Parse(f); err != nil {\n    return err\n}\nif p.lerr != nil {\n    log.Printf(\"stopped at %v\", p.lerr)\n}","preventionTips":["Skip blank lines inside Parse before incrementing p.lines","Pre-scan the file format before parsing","Always check p.lerr after the parse loop","Include the raw line in p.lerr for diagnosis"],"tags":["go","parsing","v4","stateful-parser"],"backgroundTag":"wrong-number-of-fields","analyzedSha":"3c475a78e54336c6255e925ff66b8ef4da6a2ae7","analyzedAt":"2026-09-02T10:14:38.492Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T16:17:10.729Z"}