From dfc5cbf6901a022d4b7abe74296760f29b88de08 Mon Sep 17 00:00:00 2001 From: "P. Barrett Little" Date: Sun, 15 Sep 2024 17:31:18 -0400 Subject: [PATCH] feat: Enhance CSV parsing with detailed error handling Improve error reporting in shot data processing by: - Including file name in error messages for better context - Adding line number tracking for precise error location - Enhancing logging with more detailed information These changes will aid in debugging and provide clearer feedback when processing CSV files, making it easier to identify and resolve issues in the input data. --- internal/parsers/shot_data_parser.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/internal/parsers/shot_data_parser.go b/internal/parsers/shot_data_parser.go index 7ef7c75..6bc508f 100644 --- a/internal/parsers/shot_data_parser.go +++ b/internal/parsers/shot_data_parser.go @@ -23,7 +23,7 @@ func ProcessShotData(inputFile string, launchMonitorType string) ([]models.Proce // Open the input file file, err := os.Open(inputFile) if err != nil { - return nil, fmt.Errorf("opening file: %w", err) + return nil, fmt.Errorf("opening file '%s': %w", inputFile, err) } defer file.Close() @@ -45,15 +45,18 @@ func ProcessShotData(inputFile string, launchMonitorType string) ([]models.Proce var shotData []models.ProcessedShotData var headers []string inDataBlock := false + lineNumber := 0 // Initialize line number // Read and process each row of the CSV file for { row, err := csvReader.Read() + lineNumber++ // Increment line number for each read operation + if err != nil { if err == io.EOF { break } - return nil, fmt.Errorf("reading row: %w", err) + return nil, fmt.Errorf("error reading row %d: %w", lineNumber, err) } if len(row) == 0 { @@ -65,6 +68,7 @@ func ProcessShotData(inputFile string, launchMonitorType string) ([]models.Proce headers = normalizeHeaders(row) inDataBlock = true logging.Debug("Found headers", logging.Fields{ + "line": lineNumber, "headers": headers, }) continue @@ -83,8 +87,9 @@ func ProcessShotData(inputFile string, launchMonitorType string) ([]models.Proce // Parse and process the row data rawData, err := launchMonitor.ParseRow(row, headers) if err != nil { - logging.Error("Skipping row due to error", err, logging.Fields{ - "row": row, + logging.Error(fmt.Sprintf("Skipping row %d due to error", lineNumber), err, logging.Fields{ + "line": lineNumber, + "row": row, }) continue } @@ -94,11 +99,12 @@ func ProcessShotData(inputFile string, launchMonitorType string) ([]models.Proce } if len(shotData) == 0 { - return nil, fmt.Errorf("no valid data found in the file") + return nil, fmt.Errorf("no valid data found in the file '%s'", inputFile) } logging.Info("Processed shot data", logging.Fields{ "shotsProcessed": len(shotData), + "file": inputFile, }) return shotData, nil