Skip to content
This repository has been archived by the owner on May 25, 2022. It is now read-only.

CSV Idiomatic Errors #323

Merged
merged 2 commits into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions operator/builtin/parser/csv/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package csv
import (
"context"
csvparser "encoding/csv"
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -51,21 +52,21 @@ func (c CSVParserConfig) Build(context operator.BuildContext) ([]operator.Operat
}

if c.Header == "" {
return nil, fmt.Errorf("Missing required field 'header'")
return nil, errors.New("missing required field 'header'")
}

if c.FieldDelimiter == "" {
c.FieldDelimiter = ","
}

if len([]rune(c.FieldDelimiter)) != 1 {
return nil, fmt.Errorf("Invalid 'delimiter': '%s'", c.FieldDelimiter)
return nil, fmt.Errorf("invalid 'delimiter': '%s'", c.FieldDelimiter)
}

fieldDelimiter := []rune(c.FieldDelimiter)[0]

if !strings.Contains(c.Header, c.FieldDelimiter) {
return nil, fmt.Errorf("missing field delimiter in header")
return nil, errors.New("missing field delimiter in header")
}

numFields := len(strings.Split(c.Header, c.FieldDelimiter))
Expand Down
2 changes: 1 addition & 1 deletion operator/builtin/parser/csv/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestCSVParserBuildFailureInvalidDelimiter(t *testing.T) {
cfg.FieldDelimiter = ";;"
_, err := cfg.Build(testutil.NewBuildContext(t))
require.Error(t, err)
require.Contains(t, err.Error(), "Invalid 'delimiter': ';;'")
require.Contains(t, err.Error(), "invalid 'delimiter': ';;'")
}

func TestCSVParserByteFailure(t *testing.T) {
Expand Down