Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function unmarshal swallows specific error message #923

Closed
ewohltman opened this issue May 3, 2021 · 0 comments · Fixed by #924
Closed

Function unmarshal swallows specific error message #923

ewohltman opened this issue May 3, 2021 · 0 comments · Fixed by #924

Comments

@ewohltman
Copy link
Contributor

ewohltman commented May 3, 2021

While recently troubleshooting an issue, I found that it was very useful to include the specific error message in the error returned by the function unmarshal(data []byte, v interface{}) error in restapi.go.

Currently, we swallow the specific error message:

func unmarshal(data []byte, v interface{}) error {
	err := json.Unmarshal(data, v)
	if err != nil {
                // err is effectively lost here
		return ErrJSONUnmarshal
	}

	return nil
}

Instead, we can wrap ErrJSONUnmarshal and include the specific error message:

func unmarshal(data []byte, v interface{}) error {
	err := json.Unmarshal(data, v)
	if err != nil {
		return fmt.Errorf("%w: %s", ErrJSONUnmarshal, err)
	}

	return nil
}

This error wrapping functionality was introduced to the standard library in Go 1.13, so I believe we should update the go.mod file as well. Background: https://blog.golang.org/go1.13-errors

module github.com/bwmarrin/discordgo

go 1.13

require (
	github.com/gorilla/websocket v1.4.2
	golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b
)

Fortunately, the CI process only tests 1.13 and later so in a sense we already no longer support Go versions less than 1.13. While we're working on this, and since Go 1.16 has been out for awhile, might as well update the CI to test with 1.16 too:

language: go
go:
    - 1.13.x
    - 1.14.x
    - 1.15.x
    - 1.16.x
env:
    - GO111MODULE=on
install:
    - go get github.com/bwmarrin/discordgo
    - go get -v .
    - go get -v golang.org/x/lint/golint
script:
    - diff <(gofmt -d .) <(echo -n)
    - go vet -x ./...
    - golint -set_exit_status ./...
    - go test -v -race ./...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant