(t *testing.T)
| 88 | } |
| 89 | |
| 90 | func TestEncodeDecodeStatus(t *testing.T) { |
| 91 | testcases := []struct { |
| 92 | desc string |
| 93 | makeStatus func(*testing.T, codes.Code, string, []proto.Message) statusIface |
| 94 | fromError func(err error) statusIface |
| 95 | expectDetails []interface{} // nil elements signify errors |
| 96 | }{ |
| 97 | { |
| 98 | desc: "gogo status", |
| 99 | makeStatus: func(t *testing.T, code codes.Code, msg string, details []proto.Message) statusIface { |
| 100 | s, err := gogostatus.New(code, msg).WithDetails(details...) |
| 101 | require.NoError(t, err) |
| 102 | return s |
| 103 | }, |
| 104 | fromError: func(err error) statusIface { |
| 105 | return gogostatus.Convert(err) |
| 106 | }, |
| 107 | expectDetails: []interface{}{ |
| 108 | nil, // Protobuf decode fails |
| 109 | &errorspb.StringsPayload{Details: []string{"foo", "bar"}}, // gogoproto succeeds |
| 110 | nil, // dummy decode fails |
| 111 | }, |
| 112 | }, |
| 113 | { |
| 114 | desc: "grpc status", |
| 115 | makeStatus: func(t *testing.T, code codes.Code, msg string, details []proto.Message) statusIface { |
| 116 | s := grpcstatus.New(code, msg) |
| 117 | for _, detail := range details { |
| 118 | var err error |
| 119 | s, err = s.WithDetails(protoiface.MessageV1(detail)) |
| 120 | require.NoError(t, err) |
| 121 | } |
| 122 | return s |
| 123 | }, |
| 124 | fromError: func(err error) statusIface { |
| 125 | return grpcstatus.Convert(err) |
| 126 | }, |
| 127 | expectDetails: []interface{}{ |
| 128 | // Protobuf succeeds |
| 129 | func() interface{} { |
| 130 | var st interface{} = grpcstatus.New(codes.Internal, "status").Proto() |
| 131 | res := reflect.New(reflect.TypeOf(st).Elem()).Interface() |
| 132 | copyPublicFields(res, st) |
| 133 | return res |
| 134 | }(), |
| 135 | nil, // gogoproto decode fails |
| 136 | nil, // dummy decode fails |
| 137 | }, |
| 138 | }, |
| 139 | } |
| 140 | for _, tc := range testcases { |
| 141 | tc := tc |
| 142 | t.Run(tc.desc, func(t *testing.T) { |
| 143 | ctx := context.Background() |
| 144 | |
| 145 | // Create a Status, using statusIface to support gRPC and gogo variants. |
| 146 | status := tc.makeStatus(t, codes.NotFound, "message", []proto.Message{ |
| 147 | grpcstatus.New(codes.Internal, "status").Proto(), // standard Protobuf |
nothing calls this directly
no test coverage detected
searching dependent graphs…