MCPcopy
hub / github.com/grpc/grpc-go / TestFromError_Wrapped

Method TestFromError_Wrapped

status/status_ext_test.go:264–317  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

262}
263
264func (s) TestFromError_Wrapped(t *testing.T) {
265 base := status.New(codes.Canceled, "inner canceled")
266 sWithDetails, err := base.WithDetails(&testpb.Empty{})
267 if err != nil {
268 t.Fatalf("WithDetails failed: %v", err)
269 }
270 innerErr := sWithDetails.Err()
271 mustStatus := func(message string) *status.Status {
272 st, err := status.New(codes.Canceled, message).WithDetails(&testpb.Empty{})
273 if err != nil {
274 t.Fatal(err)
275 }
276 return st
277 }
278
279 testCases := []struct {
280 name string
281 err error
282 wantStatus *status.Status
283 }{
284 {
285 name: "direct_error",
286 err: innerErr,
287 wantStatus: mustStatus("inner canceled"),
288 },
289 {
290 name: "wrapped_error",
291 err: fmt.Errorf("wrapped: %w", innerErr),
292 wantStatus: mustStatus("wrapped: rpc error: code = Canceled desc = inner canceled"),
293 },
294 {
295 name: "double_wrapped_error",
296 err: fmt.Errorf("outer: %w", fmt.Errorf("inner: %w", innerErr)),
297 wantStatus: mustStatus("outer: inner: rpc error: code = Canceled desc = inner canceled"),
298 },
299 {
300 name: "double_wrapped_single_errorf",
301 err: fmt.Errorf("error: %w: %w", errors.New("test error"), innerErr),
302 wantStatus: mustStatus("error: test error: rpc error: code = Canceled desc = inner canceled"),
303 },
304 }
305
306 for _, tc := range testCases {
307 t.Run(tc.name, func(t *testing.T) {
308 got, ok := status.FromError(tc.err)
309 if !ok {
310 t.Fatalf("status.FromError(%v) returned false; want true", tc.err)
311 }
312 if diff := cmp.Diff(tc.wantStatus, got, protocmp.Transform(), cmp.AllowUnexported(status.Status{})); diff != "" {
313 t.Fatalf("status.FromError(%v) got unexpected output, diff (-want +got):\n%s", tc.err, diff)
314 }
315 })
316 }
317}

Callers

nothing calls this directly

Calls 7

NewFunction · 0.92
FromErrorFunction · 0.92
WithDetailsMethod · 0.80
ErrMethod · 0.80
FatalfMethod · 0.65
FatalMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected