Skip to content

Commit

Permalink
Add details to stream error response.
Browse files Browse the repository at this point in the history
  • Loading branch information
johanbrandhorst committed Feb 28, 2018
1 parent 4eed2ba commit 345e924
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 23 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
18 changes: 14 additions & 4 deletions runtime/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"net/http"
"net/textproto"

"github.com/golang/protobuf/ptypes/any"

"github.com/golang/protobuf/proto"
"github.com/grpc-ecosystem/grpc-gateway/runtime/internal"
"golang.org/x/net/context"
Expand Down Expand Up @@ -168,14 +170,22 @@ func handleForwardResponseStreamError(wroteHeader bool, marshaler Marshaler, w h

func streamChunk(result proto.Message, err error) map[string]proto.Message {
if err != nil {
s, _ := status.FromError(err)
httpCode := HTTPStatusFromCode(s.Code())
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(s.Code()),
GrpcCode: int32(grpcCode),
HttpCode: int32(httpCode),
Message: s.Message(),
Message: grpcMessage,
HttpStatus: http.StatusText(httpCode),
Details: grpcDetails,
},
}
}
Expand Down
1 change: 1 addition & 0 deletions runtime/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func TestForwardResponseStream(t *testing.T) {
HttpCode: int32(httpCode),
Message: st.Message(),
HttpStatus: http.StatusText(httpCode),
Details: st.Proto().GetDetails(),
},
})
if err != nil {
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 345e924

Please sign in to comment.