Skip to content

Commit

Permalink
function/stdlib: Consistent csvdecode error messages
Browse files Browse the repository at this point in the history
Go 1.17 changes the way the CSV parser reports columns in its error
objects, causing our error results to be inconsistent depending on which
Go version we're built with.

In order to make the error messages consistent we'll use our own custom
error formatter for that case. This does mean we lose the column number
in the error message, but we'll still include the line number where the
problem was detected.
  • Loading branch information
apparentlymart committed Jun 28, 2021
1 parent 84ff582 commit 47a7961
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 11 additions & 2 deletions cty/function/stdlib/csv.go
Expand Up @@ -30,7 +30,7 @@ var CSVDecodeFunc = function.New(&function.Spec{
return cty.DynamicPseudoType, fmt.Errorf("missing header line")
}
if err != nil {
return cty.DynamicPseudoType, err
return cty.DynamicPseudoType, csvError(err)
}

atys := make(map[string]cty.Type, len(headers))
Expand Down Expand Up @@ -64,7 +64,7 @@ var CSVDecodeFunc = function.New(&function.Spec{
break
}
if err != nil {
return cty.DynamicVal, err
return cty.DynamicVal, csvError(err)
}

vals := make(map[string]cty.Value, len(cols))
Expand All @@ -91,3 +91,12 @@ var CSVDecodeFunc = function.New(&function.Spec{
func CSVDecode(str cty.Value) (cty.Value, error) {
return CSVDecodeFunc.Call([]cty.Value{str})
}

func csvError(err error) error {
switch err := err.(type) {
case *csv.ParseError:
return fmt.Errorf("CSV parse error on line %d: %w", err.Line, err.Err)
default:
return err
}
}
2 changes: 1 addition & 1 deletion cty/function/stdlib/csv_test.go
Expand Up @@ -58,7 +58,7 @@ func TestCSVDecode(t *testing.T) {
{
cty.StringVal(`invalid"thing"`),
cty.DynamicVal,
`parse error on line 1, column 7: bare " in non-quoted-field`,
`CSV parse error on line 1: bare " in non-quoted-field`,
},
{
cty.UnknownVal(cty.String),
Expand Down

0 comments on commit 47a7961

Please sign in to comment.