{"record":{"id":"01f557477a9cb03a","repo":"inancgumus/learngo","slug":"wrong-input-v-line-d-01f557","errorCode":null,"errorMessage":"wrong input: %v (line #%d)","messagePattern":"wrong input: (.+?) \\(line #(.+?)\\)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"logparser/v3/parser.go","lineNumber":41,"sourceCode":"\n// parser keep tracks of the parsing\ntype parser struct {\n\tsum     map[string]result // metrics per domain\n\tdomains []string          // unique domain names\n\ttotal   int               // total visits for all domains\n\tlines   int               // number of parsed lines (for the error messages)\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) (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","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/inancgumus/learngo/blob/3c475a78e54336c6255e925ff66b8ef4da6a2ae7/logparser/v3/parser.go#L23-L59","documentation":"In the v3 parser, parse accepts lines of exactly 2 fields (domain and visits). Any other field count sets the returned error 'wrong input: %v (line #%d)', embedding the parsed fields slice and the current line number p.lines, and returns a zero result.","triggerScenarios":"Calling parse (via main's per-line loop) with a line that splits into 0, 1, or 3+ tokens — e.g. a domain-only line, or a line with domain visits plus an extra column.","commonSituations":"Mixed log versions where some lines still carry a third column (e.g. time spent from the older 3-field format), blank lines, or extra whitespace-separated annotations.","solutions":["Ensure the input file uses the 2-field 'domain visits' format throughout","Filter or convert 3-field legacy lines before parsing","Improve the message to state the expected format explicitly"],"exampleFix":"// before\nif len(fields) != 2 {\n    err = fmt.Errorf(\"wrong input: %v (line #%d)\", fields, p.lines)\n    return\n}\n// after\nif len(fields) != 2 {\n    err = fmt.Errorf(\"wrong input on line #%d: expected 'domain visits', got %q\", p.lines, line)\n    return\n}","handlingStrategy":"validation","validationCode":"func validVisitLine(line string) bool {\n    fs := strings.Fields(line)\n    return len(fs) == 2\n}\n// filter lines before parse(p, line)","typeGuard":"func hasTwoFields(line string) bool {\n    return len(strings.Fields(line)) == 2\n}","tryCatchPattern":"parsed, err := parse(p, line)\nif err != nil {\n    if strings.Contains(err.Error(), \"wrong input\") {\n        log.Printf(\"skipping: %v\", err)\n        continue\n    }\n    return err\n}","preventionTips":["Convert legacy 3-field lines to 2-field format before parsing","Skip blank lines before counting them as records","Include the raw line (not just fields) in error output","Test the parser against mixed-format fixtures"],"tags":["go","parsing","v3"],"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"}