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 1 commit
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
18 changes: 15 additions & 3 deletions connect_ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,13 +690,16 @@

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)

assertInternalError := func(tb testing.TB, opts ...connect.ClientOption) {

Check failure on line 702 in connect_ext_test.go

View workflow job for this annotation

GitHub Actions / ci (1.22.x)

test helper function should start from tb.Helper() (thelper)
tb.Helper()
emcfarlane marked this conversation as resolved.
Show resolved Hide resolved
client := pingv1connect.NewPingServiceClient(server.Client(), server.URL(), opts...)
request := connect.NewRequest(&pingv1.FailRequest{Code: int32(connect.CodeResourceExhausted)})
_, err := client.Fail(context.Background(), request)
Expand All @@ -705,6 +708,7 @@
var connectErr *connect.Error
ok := errors.As(err, &connectErr)
assert.True(t, ok)
// 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.True(
t,
Expand Down Expand Up @@ -2609,7 +2613,8 @@
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 +2651,13 @@
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
39 changes: 23 additions & 16 deletions protocol_grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -843,28 +843,35 @@ func grpcErrorToTrailer(trailer http.Header, protobuf Codec, err error) {
}
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
var bin []byte
emcfarlane marked this conversation as resolved.
Show resolved Hide resolved
if len(status.Details) > 0 {
// attempt to serialize the Status first, so we can return an error before setting other headers
var binErr error
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)
}
emcfarlane marked this conversation as resolved.
Show resolved Hide resolved
setHeaderCanonical(trailer, grpcHeaderStatus, code)
setHeaderCanonical(trailer, grpcHeaderMessage, grpcPercentEncode(status.GetMessage()))
setHeaderCanonical(trailer, grpcHeaderDetails, EncodeBinaryHeader(bin))
if len(bin) > 0 {
setHeaderCanonical(trailer, grpcHeaderDetails, EncodeBinaryHeader(bin))
}
}

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