go-errors / errors

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

nil error always fail at err != nil check #32

Closed meetme2meat closed 3 years ago

meetme2meat commented 3 years ago

Following is the sample code the output would say a lot here. I tried debugging this myself but unable to find why the behavior is such.

func main() {
    err := DBErr()
    if err != nil {
        fmt.Printf("Main err was not nil %T %v\n", err, err)
    } else {
        fmt.Println("Main", "err was nil")
    }
}

func DBErr() error {
    e := errors.Wrap(nil, 1)
    fmt.Println("DBErr() err is nil", e == nil)
    return e
}

Output:

DBErr() err is nil true
Main err was not nil *errors.Error <nil>

running go version

go version go1.16 darwin/amd64

following is my go.mod definition

module test-errors

go 1.16

require github.com/go-errors/errors v1.2.0