{"record":{"id":"f10326c04251babd","repo":"inancgumus/learngo","slug":"line-d-v","errorCode":null,"errorMessage":"line %d: %v","messagePattern":"line (.+?): (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"logparser/functional/textreader.go","lineNumber":28,"sourceCode":"\nimport (\n\t\"bufio\"\n\t\"fmt\"\n\t\"io\"\n)\n\nfunc textReader(r io.Reader) inputFn {\n\treturn func(process processFn) error {\n\t\tvar (\n\t\t\tl  = 1\n\t\t\tin = bufio.NewScanner(r)\n\t\t)\n\n\t\tfor in.Scan() {\n\t\t\tr, err := fastParseFields(in.Bytes())\n\t\t\t// r, err := parseFields(in.Text())\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"line %d: %v\", l, err)\n\t\t\t}\n\n\t\t\tprocess(r)\n\t\t\tl++\n\t\t}\n\n\t\tif c, ok := r.(io.Closer); ok {\n\t\t\tc.Close()\n\t\t}\n\t\treturn in.Err()\n\t}\n}\n","sourceCodeStart":10,"sourceCodeEnd":41,"githubUrl":"https://github.com/inancgumus/learngo/blob/3c475a78e54336c6255e925ff66b8ef4da6a2ae7/logparser/functional/textreader.go#L10-L41","documentation":"The text reader scans the input line by line and calls fastParseFields on each. When field parsing fails it stops immediately and wraps the underlying error with the 1-based line number (l), so callers know exactly which line of the file was bad. The error message is 'line %d: %v'.","triggerScenarios":"Reading a log file where any single line fails fastParseFields — wrong field count (error 30), or any other parse error — causes Read/process to return this wrapped error for that line number.","commonSituations":"Malformed line in the middle of a large log file; a different log format mixed in; a partially written last line; the file using an unexpected delimiter so fields don't split as expected.","solutions":["Read the line number from the error and fix that line in the log file","Skip malformed lines: accumulate errors per line and continue instead of returning on the first one","Make fastParseFields error messages include the raw line for easier diagnosis"],"exampleFix":"// before\nif err != nil {\n    return fmt.Errorf(\"line %d: %v\", l, err)\n}\n// after\nif err != nil {\n    badLines = append(badLines, fmt.Sprintf(\"line %d: %v\", l, err))\n    continue // collect all errors instead of failing on the first\n}","handlingStrategy":"validation","validationCode":"scanner := bufio.NewScanner(f)\nfor scanner.Scan() {\n    if len(strings.Fields(scanner.Text())) != expected {\n        log.Printf(\"skipping line %d\", l)\n        continue\n    }\n    ...\n}","typeGuard":"func isParsableLine(text string, want int) bool {\n    return len(strings.Fields(text)) == want\n}","tryCatchPattern":"if err := readLines(f, process); err != nil {\n    var lineErr LineError\n    if errors.As(err, &lineErr) {\n        log.Printf(\"failed at line %d: %v\", lineErr.Num, lineErr.Err)\n    } else {\n        return err\n    }\n}","preventionTips":["Sanitize input files (strip blanks, fix truncations) before reading","Collect per-line errors instead of aborting on the first one","Keep the line number in every wrapped error (already done here)","Log the offending line content alongside the line number"],"tags":["go","parsing","error-wrapping"],"backgroundTag":"log-line-parse-error","analyzedSha":"3c475a78e54336c6255e925ff66b8ef4da6a2ae7","analyzedAt":"2026-09-02T10:14:38.492Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T16:17:10.729Z"}