go-errors / errors

errors with stacktraces for go
https://godoc.org/github.com/go-errors/errors
MIT License
935 stars 93 forks source link

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

Open jeanspector-google opened 3 years ago

jeanspector-google commented 3 years ago

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 commented 3 years ago

Sent pull request #36 that should fix the issue