| 1 |
package grpcstatus |
| 2 |
|
| 3 |
import ( |
| 4 |
"github.com/stretchr/testify/require" |
| 5 |
"google.golang.org/grpc/codes" |
| 6 |
"google.golang.org/grpc/status" |
| 7 |
"testing" |
| 8 |
) |
| 9 |
|
| 10 |
// Own implementation of pkg/errors withStack to avoid additional dependency |
| 11 |
type wrappedError struct { |
| 12 |
cause error |
| 13 |
msg string |
| 14 |
} |
| 15 |
|
| 16 |
func (w *wrappedError) Error() string { return w.msg + ": " + w.cause.Error() } |
| 17 |
|
| 18 |
func (w *wrappedError) Cause() error { return w.cause } |
| 19 |
|
| 20 |
func TestErrorUnwrapping(t *testing.T) { |
| 21 |
gRPCCode := codes.FailedPrecondition |
| 22 |
gRPCError := status.Errorf(gRPCCode, "Userspace error.") |
| 23 |
expectedGRPCStatus, _ := status.FromError(gRPCError) |
| 24 |
testedErrors := []error{ |
| 25 |
gRPCError, |
| 26 |
&wrappedError{cause: gRPCError, msg: "pkg/errors wrapped error: "}, |
| 27 |
} |
| 28 |
|
| 29 |
for _, e := range testedErrors { |
| 30 |
resultingStatus, ok := FromError(e) |
| 31 |
require.True(t, ok) |
| 32 |
require.Equal(t, expectedGRPCStatus, resultingStatus) |
| 33 |
} |
| 34 |
} |