Skip to content

Commit

Permalink
encoding/jsonfile: stutter less (JSON is already mentioned in inner…
Browse files Browse the repository at this point in the history
… error)
  • Loading branch information
joonas-fi committed Jul 12, 2023
1 parent 6f1204d commit a938b0d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion encoding/jsonfile/jsonfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ func unmarshalInternal(source io.Reader, data interface{}, disallowUnknownFields
if err := jsonDecoder.Decode(data); err != nil {
// sadly, line numbers are only possible with hacks (requiring buffering):
// https://github.com/hashicorp/packer/blob/master/common/json/unmarshal.go
return fmt.Errorf("JSON parsing failed: %s", err.Error())
//
// no need mention JSON as context in error message, because `err` already mentions that.
return fmt.Errorf("decode failed: %s", err.Error())
}

return nil
Expand Down
12 changes: 12 additions & 0 deletions encoding/jsonfile/jsonfile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package jsonfile

import (
"testing"

"github.com/function61/gokit/testing/assert"
)

func TestRead(t *testing.T) {
assert.Equal(t, ReadDisallowUnknownFields("notfound.json", &struct{}{}).Error(), "open notfound.json: no such file or directory")
assert.Equal(t, ReadDisallowUnknownFields("testdata/example.json", &struct{}{}).Error(), `testdata/example.json: decode failed: json: unknown field "foo"`)
}
3 changes: 3 additions & 0 deletions encoding/jsonfile/testdata/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"foo": "bar"
}

0 comments on commit a938b0d

Please sign in to comment.