Skip to content

Commit

Permalink
feat: add exception for encoding/json.decoder.Token
Browse files Browse the repository at this point in the history
As noted in https://pkg.go.dev/encoding/json#Decoder.Token,
it can return io.EOF.

Add a test case.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
  • Loading branch information
kolyshkin authored and polyfloyd committed Aug 10, 2024
1 parent bbdc417 commit 47749f4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions errorlint/allowed.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func setDefaultAllowedErrors() {
{Err: "context.Canceled", Fun: "(context.Context).Err"},
// pkg/encoding/json
{Err: "io.EOF", Fun: "(*encoding/json.Decoder).Decode"},
{Err: "io.EOF", Fun: "(*encoding/json.Decoder).Token"},
// pkg/encoding/csv
{Err: "io.EOF", Fun: "(*encoding/csv.Reader).Read"},
// pkg/mime/multipart
Expand Down
11 changes: 8 additions & 3 deletions errorlint/testdata/src/allowed/allowed.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,16 @@ func ContextErr(ctx context.Context) error {
return nil
}

func JSONReader(r io.Reader) {
err := json.NewDecoder(r).Decode(nil)
func JSONReader(dec *json.Decoder) (err error) {
_, err = dec.Token()
if err == io.EOF {
fmt.Println(err)
return
}
err = dec.Decode(nil)
if err == io.EOF {
return
}
return
}

func CSVReader(r io.Reader) {
Expand Down

0 comments on commit 47749f4

Please sign in to comment.