Skip to content

nil check when wrapping errors fails for custom error types #35

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

Open
jeanspector-google opened this issue Jun 8, 2021 · 1 comment
Open

Comments

@jeanspector-google
Copy link

nil check when wrapping errors fails for custom error types. nil error get wrapped and - with pointers - it even results in a panic.

Here's a UT that demonstrates the behavior:

package go_err

import (
	"testing"

	"github.com/go-errors/errors"
)

type MyError1 struct {
	message string
}

func (me1 MyError1) Error() string{
	return me1.message
}

type MyError2 struct {
	message string
}

func (me2 *MyError2) Error() string{
	return me2.message
}

func TestNilErr(t *testing.T) {
	var (
		err error
		myErr1 MyError1
		myErr2 *MyError2
	)

	err = errors.WrapPrefix(err, "blah message", 0)
	t.Log("base error", "value", err)

	// Shouldn't be wrapped, but it is
	err = errors.WrapPrefix(myErr1, "blah message", 0)
	t.Log("my error 1", "value", err)

	// Shouldn't be wrapped, but it panics
	err = errors.WrapPrefix(myErr2, "blah message", 0)
	t.Log("my error 2", "value", err)
}
@jeanspector-google
Copy link
Author

Sent pull request #36 that should fix the issue

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

No branches or pull requests

1 participant