Skip to content

Commit

Permalink
[pkg/stanza] Enhance error message in csv_parser (#13432)
Browse files Browse the repository at this point in the history
When this error is triggered, it is very helpful to know what was
expected and found.
  • Loading branch information
djaglowski authored Aug 19, 2022
1 parent 976fab3 commit 8a821bb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkg/stanza/operator/parser/csv/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func generateParseFunc(headers []string, fieldDelimiter rune, lazyQuotes bool) p
parsedValues := make(map[string]interface{})

if len(joinedLine) != len(headers) {
return nil, errors.New("wrong number of fields")
return nil, fmt.Errorf("wrong number of fields: expected %d, found %d", len(headers), len(joinedLine))
}

for i, val := range joinedLine {
Expand Down
4 changes: 2 additions & 2 deletions pkg/stanza/operator/parser/csv/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ func TestParserByteFailure(t *testing.T) {
parser := newTestParser(t)
_, err := parser.parse([]byte("invalid"))
require.Error(t, err)
require.Contains(t, err.Error(), "wrong number of fields")
require.Contains(t, err.Error(), "wrong number of fields: expected 3, found 1")
}

func TestParserStringFailure(t *testing.T) {
parser := newTestParser(t)
_, err := parser.parse("invalid")
require.Error(t, err)
require.Contains(t, err.Error(), "wrong number of fields")
require.Contains(t, err.Error(), "wrong number of fields: expected 3, found 1")
}

func TestParserInvalidType(t *testing.T) {
Expand Down
16 changes: 16 additions & 0 deletions unreleased/pkg-stanza-csv-err.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: "`filelog`, `journald`, `syslog`, `tcplog`, `udplog`, `windowseventlog` receivers"

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Enhance error message when csv_parser finds unexpected number of fields

# One or more tracking issues related to the change
issues: [13427]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

0 comments on commit 8a821bb

Please sign in to comment.