We have one function that returns errors one of them is wrapped by errors.Wrap(), the others are not.
var ErrTest1 = errors.New("error test 1")
var ErrTest2 = errors.New("error test 2")
var ErrRPC = errors.New("error rpc")
func rpcCall() error {
return ErrRPC
}
func testErrWrap(a int) error {
if a == 1 {
return ErrTest1
} else if a == 2 {
return ErrTest2
} else {
err := rpcCall()
if err != nil {
return errors.Wrap(ErrRPC, "call rpc err")
}
}
return nil
}
We have two solutions, one is
err := testErrWrap(3)
if errors.Unwrap(err) != nil {
fmt.Println(errors.Unwrap(err))
}
the other is
err := testErrWrap(3)
if !errors.Is(err, ErrTest2) && !errors.Is(err, ErrTest1) {
tErr := errors.Unwrap(err)
fmt.Println(tErr)
}
We want to know the elegant way to distinguish errors are wrapped or not in Go?