(
ctx context.Context,
req interface{},
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler,
)
| 11 | ) |
| 12 | |
| 13 | func UnaryServerInterceptor( |
| 14 | ctx context.Context, |
| 15 | req interface{}, |
| 16 | info *grpc.UnaryServerInfo, |
| 17 | handler grpc.UnaryHandler, |
| 18 | ) (interface{}, error) { |
| 19 | resp, err := handler(ctx, req) |
| 20 | if err == nil { |
| 21 | return resp, err |
| 22 | } |
| 23 | |
| 24 | st, ok := status.FromError(err) |
| 25 | if !ok { |
| 26 | code := extgrpc.GetGrpcCode(err) |
| 27 | st = status.New(code, err.Error()) |
| 28 | enc := errors.EncodeError(ctx, err) |
| 29 | st, err = st.WithDetails(&enc) |
| 30 | if err != nil { |
| 31 | |
| 32 | // https://jbrandhorst.com/post/grpc-errors/ |
| 33 | // "If this errored, it will always error |
| 34 | // here, so better panic so we can figure |
| 35 | // out why than have this silently passing." |
| 36 | // |
| 37 | // More specifically, an error here is from ptypes.MarshalAny(detail), which probably |
| 38 | // means that your proto.Message is not registered with gogoproto. (Make sure that |
| 39 | // your error's .pb.go file imports "github.com/gogo/protobuf/proto".) |
| 40 | // |
| 41 | // By panicking, we either take down the service or (if it has a recovery middleware) cause |
| 42 | // the call to fail dramatically. Either case will draw attention to get it fixed. |
| 43 | // |
| 44 | // If we simply returned an errors.AssertionFailed, our entire error stack would vanish |
| 45 | // as it crosses the network boundary. A client would receive a grpc status with code.Internal, |
| 46 | // and the stringification of the error. This change in behavior could induce subtle bugs |
| 47 | // in the client since none of the usual errors are being returned. |
| 48 | // |
| 49 | // We could also log the error here via whatever appropriate mechanism, but the truth is |
| 50 | // that the service was seriously misconfigured and shouldn't be running at all. |
| 51 | // |
| 52 | panic(err) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return resp, st.Err() |
| 57 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…