|
| 1 | +package grpcerrors_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/moby/buildkit/util/grpcerrors" |
| 10 | + "github.com/pkg/errors" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + spb "google.golang.org/genproto/googleapis/rpc/status" |
| 14 | + "google.golang.org/grpc/codes" |
| 15 | + "google.golang.org/grpc/status" |
| 16 | + "google.golang.org/protobuf/types/known/anypb" |
| 17 | +) |
| 18 | + |
| 19 | +func TestFromGRPCPreserveUnknownTypes(t *testing.T) { |
| 20 | + t.Parallel() |
| 21 | + const ( |
| 22 | + unknownType = "type.googleapis.com/unknown.Type" |
| 23 | + unknownValue = "unknown value" |
| 24 | + |
| 25 | + errMessage = "something failed" |
| 26 | + errCode = codes.Internal |
| 27 | + ) |
| 28 | + raw := &anypb.Any{ |
| 29 | + TypeUrl: unknownType, |
| 30 | + Value: []byte(unknownValue), |
| 31 | + } |
| 32 | + |
| 33 | + pb := &spb.Status{ |
| 34 | + Code: int32(errCode), |
| 35 | + Message: errMessage, |
| 36 | + Details: []*anypb.Any{raw}, |
| 37 | + } |
| 38 | + |
| 39 | + assertErrorProperties := func(t *testing.T, err error) { |
| 40 | + require.Error(t, err) |
| 41 | + assert.Equal(t, fmt.Sprintf("%s: %s", codes.Code(errCode), errMessage), err.Error()) |
| 42 | + |
| 43 | + st, ok := status.FromError(err) |
| 44 | + require.True(t, ok) |
| 45 | + |
| 46 | + details := st.Proto().Details |
| 47 | + require.Len(t, details, 1) |
| 48 | + assert.Equal(t, unknownType, details[0].TypeUrl) |
| 49 | + assert.Equal(t, []byte(unknownValue), details[0].Value) |
| 50 | + } |
| 51 | + |
| 52 | + t.Run("encode", func(t *testing.T) { |
| 53 | + t.Parallel() |
| 54 | + err := grpcerrors.FromGRPC(status.FromProto(pb).Err()) |
| 55 | + assertErrorProperties(t, err) |
| 56 | + }) |
| 57 | + |
| 58 | + t.Run("roundtrip", func(t *testing.T) { |
| 59 | + t.Parallel() |
| 60 | + |
| 61 | + decodedErr := grpcerrors.FromGRPC(status.FromProto(pb).Err()) |
| 62 | + |
| 63 | + reEncodedErr := grpcerrors.ToGRPC(context.TODO(), decodedErr) |
| 64 | + |
| 65 | + reDecodedErr := grpcerrors.FromGRPC(reEncodedErr) |
| 66 | + assertErrorProperties(t, reDecodedErr) |
| 67 | + }) |
| 68 | +} |
| 69 | + |
| 70 | +func TestToGRPCMessage(t *testing.T) { |
| 71 | + t.Parallel() |
| 72 | + t.Run("avoid prepending grpc status code", func(t *testing.T) { |
| 73 | + t.Parallel() |
| 74 | + err := errors.New("something") |
| 75 | + decoded := grpcerrors.FromGRPC(grpcerrors.ToGRPC(context.TODO(), err)) |
| 76 | + assert.Equal(t, err.Error(), decoded.Error()) |
| 77 | + }) |
| 78 | + t.Run("keep extra context", func(t *testing.T) { |
| 79 | + t.Parallel() |
| 80 | + err := errors.New("something") |
| 81 | + wrapped := errors.Wrap(grpcerrors.ToGRPC(context.TODO(), err), "extra context") |
| 82 | + // Check that wrapped.Error() starts with "extra context" |
| 83 | + assert.True(t, strings.HasPrefix(wrapped.Error(), "extra context"), "expected wrapped error to start with 'extra context'") |
| 84 | + encoded := grpcerrors.ToGRPC(context.TODO(), wrapped) |
| 85 | + decoded := grpcerrors.FromGRPC(encoded) |
| 86 | + assert.Equal(t, wrapped.Error(), decoded.Error()) |
| 87 | + }) |
| 88 | +} |
0 commit comments