Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only send a serialized Status in the gRPC protocol if it has details #713

Merged
merged 2 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions connect_ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,11 @@ func TestGRPCMarshalStatusError(t *testing.T) {

mux := http.NewServeMux()
mux.Handle(pingv1connect.NewPingServiceHandler(
pingServer{},
pingServer{
// Include error details in the response, so that the Status protobuf will be marshaled.
includeErrorDetails: true,
},
// We're using a codec that will fail to marshal the Status protobuf, which means the returned error will be ignored
connect.WithCodec(failCodec{}),
))
server := memhttptest.NewServer(t, mux)
Expand All @@ -701,11 +705,12 @@ func TestGRPCMarshalStatusError(t *testing.T) {
request := connect.NewRequest(&pingv1.FailRequest{Code: int32(connect.CodeResourceExhausted)})
_, err := client.Fail(context.Background(), request)
tb.Log(err)
assert.NotNil(t, err)
assert.NotNil(t, err, assert.Sprintf("expected an error"))
var connectErr *connect.Error
ok := errors.As(err, &connectErr)
assert.True(t, ok)
assert.Equal(t, connectErr.Code(), connect.CodeInternal)
assert.True(t, ok, assert.Sprintf("expected the error to be a connect.Error"))
// This should be Internal, not ResourceExhausted, because we're testing when the Status object itself fails to marshal
assert.Equal(t, connectErr.Code(), connect.CodeInternal, assert.Sprintf("expected the error code to be Internal, was %s", connectErr.Code()))
assert.True(
t,
strings.HasSuffix(connectErr.Message(), ": boom"),
Expand Down Expand Up @@ -2609,7 +2614,8 @@ func expectMetadata(meta http.Header, metaType, key, value string) error {
type pingServer struct {
pingv1connect.UnimplementedPingServiceHandler

checkMetadata bool
checkMetadata bool
includeErrorDetails bool
}

func (p pingServer) Ping(ctx context.Context, request *connect.Request[pingv1.PingRequest]) (*connect.Response[pingv1.PingResponse], error) {
Expand Down Expand Up @@ -2646,6 +2652,13 @@ func (p pingServer) Fail(ctx context.Context, request *connect.Request[pingv1.Fa
err := connect.NewError(connect.Code(request.Msg.GetCode()), errors.New(errorMessage))
err.Meta().Set(handlerHeader, headerValue)
err.Meta().Set(handlerTrailer, trailerValue)
if p.includeErrorDetails {
detail, derr := connect.NewErrorDetail(&pingv1.FailRequest{Code: request.Msg.GetCode()})
if derr != nil {
return nil, derr
}
err.AddDetail(detail)
}
return nil, err
}

Expand Down
41 changes: 19 additions & 22 deletions protocol_grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -838,33 +838,30 @@ func grpcContentTypeFromCodecName(web bool, name string) string {
func grpcErrorToTrailer(trailer http.Header, protobuf Codec, err error) {
if err == nil {
setHeaderCanonical(trailer, grpcHeaderStatus, "0") // zero is the gRPC OK status
setHeaderCanonical(trailer, grpcHeaderMessage, "")
return
}
status := grpcStatusFromError(err)
code := strconv.Itoa(int(status.GetCode()))
bin, binErr := protobuf.Marshal(status)
if binErr != nil {
setHeaderCanonical(
trailer,
grpcHeaderStatus,
strconv.FormatInt(int64(CodeInternal), 10 /* base */),
)
setHeaderCanonical(
trailer,
grpcHeaderMessage,
grpcPercentEncode(
fmt.Sprintf("marshal protobuf status: %v", binErr),
),
)
return
}
if connectErr, ok := asError(err); ok {
mergeHeaders(trailer, connectErr.meta)
}
setHeaderCanonical(trailer, grpcHeaderStatus, code)
setHeaderCanonical(trailer, grpcHeaderMessage, grpcPercentEncode(status.GetMessage()))
setHeaderCanonical(trailer, grpcHeaderDetails, EncodeBinaryHeader(bin))
var (
status = grpcStatusFromError(err)
code = status.GetCode()
message = status.GetMessage()
bin []byte
)
if len(status.Details) > 0 {
var binErr error
bin, binErr = protobuf.Marshal(status)
if binErr != nil {
code = int32(CodeInternal)
message = fmt.Sprintf("marshal protobuf status: %v", binErr)
}
}
setHeaderCanonical(trailer, grpcHeaderStatus, strconv.Itoa(int(code)))
setHeaderCanonical(trailer, grpcHeaderMessage, grpcPercentEncode(message))
if len(bin) > 0 {
setHeaderCanonical(trailer, grpcHeaderDetails, EncodeBinaryHeader(bin))
}
}

func grpcStatusFromError(err error) *statusv1.Status {
Expand Down
Loading