Skip to content

Commit

Permalink
Add details to stream error response (grpc-ecosystem#561)
Browse files Browse the repository at this point in the history
* Use the status Message when possible in errors
* Add details to stream error response.
  • Loading branch information
johanbrandhorst authored and achew22 committed Mar 7, 2018
1 parent 10bbbbd commit 6932f71
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 30 deletions.
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

PKG=github.com/grpc-ecosystem/grpc-gateway
GO_PLUGIN=bin/protoc-gen-go
GO_PLUGIN_PKG=github.com/golang/protobuf/protoc-gen-go
GO_PROTOBUF_REPO=github.com/golang/protobuf
GO_PLUGIN_PKG=$(GO_PROTOBUF_REPO)/protoc-gen-go
GO_PTYPES_ANY_PKG=$(GO_PROTOBUF_REPO)/ptypes/any
SWAGGER_PLUGIN=bin/protoc-gen-swagger
SWAGGER_PLUGIN_SRC= utilities/doc.go \
utilities/pattern.go \
Expand Down Expand Up @@ -91,7 +93,7 @@ $(GO_PLUGIN):
go build -o $@ $(GO_PLUGIN_PKG)

$(RUNTIME_GO): $(RUNTIME_PROTO) $(GO_PLUGIN)
protoc -I $(PROTOC_INC_PATH) --plugin=$(GO_PLUGIN) -I. --go_out=$(PKGMAP):. $(RUNTIME_PROTO)
protoc -I $(PROTOC_INC_PATH) --plugin=$(GO_PLUGIN) -I $(GOPATH)/src/$(GO_PTYPES_ANY_PKG) -I. --go_out=$(PKGMAP):. $(RUNTIME_PROTO)

$(OPENAPIV2_GO): $(OPENAPIV2_PROTO) $(GO_PLUGIN)
protoc -I $(PROTOC_INC_PATH) --plugin=$(GO_PLUGIN) -I. --go_out=$(PKGMAP):$(GOPATH)/src $(OPENAPIV2_PROTO)
Expand Down
10 changes: 8 additions & 2 deletions runtime/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/textproto"

"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes/any"
"github.com/grpc-ecosystem/grpc-gateway/runtime/internal"
"golang.org/x/net/context"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -42,7 +43,7 @@ func ForwardResponseStream(ctx context.Context, mux *ServeMux, marshaler Marshal
if d, ok := marshaler.(Delimited); ok {
delimiter = d.Delimiter()
} else {
delimiter = []byte("\n")
delimiter = []byte("\n")
}

var wroteHeader bool
Expand Down Expand Up @@ -169,16 +170,21 @@ func handleForwardResponseStreamError(wroteHeader bool, marshaler Marshaler, w h
func streamChunk(result proto.Message, err error) map[string]proto.Message {
if err != nil {
grpcCode := codes.Unknown
grpcMessage := err.Error()
var grpcDetails []*any.Any
if s, ok := status.FromError(err); ok {
grpcCode = s.Code()
grpcMessage = s.Message()
grpcDetails = s.Proto().GetDetails()
}
httpCode := HTTPStatusFromCode(grpcCode)
return map[string]proto.Message{
"error": &internal.StreamError{
GrpcCode: int32(grpcCode),
HttpCode: int32(httpCode),
Message: err.Error(),
Message: grpcMessage,
HttpStatus: http.StatusText(httpCode),
Details: grpcDetails,
},
}
}
Expand Down
42 changes: 33 additions & 9 deletions runtime/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import (
"github.com/golang/protobuf/proto"
pb "github.com/grpc-ecosystem/grpc-gateway/examples/examplepb"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/grpc-ecosystem/grpc-gateway/runtime/internal"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func TestForwardResponseStream(t *testing.T) {
Expand Down Expand Up @@ -84,9 +86,32 @@ func TestForwardResponseStream(t *testing.T) {
w.Body.Close()

var want []byte
for _, msg := range tt.msgs {
for i, msg := range tt.msgs {
if msg.err != nil {
t.Skip("checking erorr encodings")
if i == 0 {
// Skip non-stream errors
t.Skip("checking error encodings")
}
st, _ := status.FromError(msg.err)
httpCode := runtime.HTTPStatusFromCode(st.Code())
b, err := marshaler.Marshal(map[string]proto.Message{
"error": &internal.StreamError{
GrpcCode: int32(st.Code()),
HttpCode: int32(httpCode),
Message: st.Message(),
HttpStatus: http.StatusText(httpCode),
Details: st.Proto().GetDetails(),
},
})
if err != nil {
t.Errorf("marshaler.Marshal() failed %v", err)
}
errBytes := body[len(want):]
if string(errBytes) != string(b) {
t.Errorf("ForwardResponseStream() = \"%s\" want \"%s\"", errBytes, b)
}

return
}
b, err := marshaler.Marshal(map[string]proto.Message{"result": msg.pb})
if err != nil {
Expand All @@ -103,17 +128,16 @@ func TestForwardResponseStream(t *testing.T) {
}
}


// A custom marshaler implementation, that doesn't implement the delimited interface
type CustomMarshaler struct {
m *runtime.JSONPb
m *runtime.JSONPb
}
func (c *CustomMarshaler) Marshal(v interface{}) ([]byte, error) { return c.m.Marshal(v) }
func (c *CustomMarshaler) Unmarshal(data []byte, v interface{}) error { return c.m.Unmarshal(data, v) }
func (c *CustomMarshaler) NewDecoder(r io.Reader) runtime.Decoder { return c.m.NewDecoder(r) }
func (c *CustomMarshaler) NewEncoder(w io.Writer) runtime.Encoder { return c.m.NewEncoder(w) }
func (c *CustomMarshaler) ContentType() string { return c.m.ContentType() }

func (c *CustomMarshaler) Marshal(v interface{}) ([]byte, error) { return c.m.Marshal(v) }
func (c *CustomMarshaler) Unmarshal(data []byte, v interface{}) error { return c.m.Unmarshal(data, v) }
func (c *CustomMarshaler) NewDecoder(r io.Reader) runtime.Decoder { return c.m.NewDecoder(r) }
func (c *CustomMarshaler) NewEncoder(w io.Writer) runtime.Encoder { return c.m.NewEncoder(w) }
func (c *CustomMarshaler) ContentType() string { return c.m.ContentType() }

func TestForwardResponseStreamCustomMarshaler(t *testing.T) {
type msg struct {
Expand Down
46 changes: 29 additions & 17 deletions runtime/internal/stream_chunk.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions runtime/internal/stream_chunk.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ syntax = "proto3";
package grpc.gateway.runtime;
option go_package = "internal";

import "google/protobuf/any.proto";

// StreamError is a response type which is returned when
// streaming rpc returns an error.
message StreamError {
int32 grpc_code = 1;
int32 http_code = 2;
string message = 3;
string http_status = 4;
repeated google.protobuf.Any details = 5;
}

0 comments on commit 6932f71

Please sign in to comment.