Skip to content

Commit

Permalink
Fix errors not being proxied to client (#45)
Browse files Browse the repository at this point in the history
* Fix errors not being proxied to client

* Add changelog entry
  • Loading branch information
bradleyjkemp authored Jul 29, 2019
1 parent 7ebe72c commit a0a8a9e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## v0.2.1 (unreleased)
## [v0.2.1](https://github.com/bradleyjkemp/grpc-tools/releases/tag/v0.2.1)
* Fixed bug where `grpc-dump` would not forward errors from server to client [#45](https://github.com/bradleyjkemp/grpc-tools/pull/45).
* `grpc-proxy` now will transparently forward all non-HTTP traffic to the original destination [#28](https://github.com/bradleyjkemp/grpc-tools/pull/28).
* When the `--proto_roots` or `--proto_descriptors` flags are used, `grpc-replay` and `grpc-fixture` will marshall messages from the human readable form instead of using the raw message.

Expand Down
4 changes: 3 additions & 1 deletion grpc-dump/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dump
import (
"github.com/bradleyjkemp/grpc-tools/grpc-proxy"
"github.com/bradleyjkemp/grpc-tools/internal/proto_decoder"
"github.com/sirupsen/logrus"
"io"
"strings"
)
Expand All @@ -24,7 +25,8 @@ func Run(output io.Writer, protoRoots, protoDescriptors string, proxyConfig ...g
resolvers = append(resolvers, r)
}

opts := append(proxyConfig, grpc_proxy.WithInterceptor(dumpInterceptor(output, proto_decoder.NewDecoder(resolvers...))))
// TODO: unify this logger with the one provided by grpc_proxy?
opts := append(proxyConfig, grpc_proxy.WithInterceptor(dumpInterceptor(logrus.New(), output, proto_decoder.NewDecoder(resolvers...))))
proxy, err := grpc_proxy.New(
opts...,
)
Expand Down
16 changes: 10 additions & 6 deletions grpc-dump/dump/dump_interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/bradleyjkemp/grpc-tools/internal"
"github.com/bradleyjkemp/grpc-tools/internal/proto_decoder"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
Expand All @@ -13,13 +14,13 @@ import (
)

// dump interceptor implements a gRPC.StreamingServerInterceptor that dumps all RPC details
func dumpInterceptor(output io.Writer, decoder proto_decoder.MessageDecoder) grpc.StreamServerInterceptor {
func dumpInterceptor(logger logrus.FieldLogger, output io.Writer, decoder proto_decoder.MessageDecoder) grpc.StreamServerInterceptor {
return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
dss := &recordedServerStream{ServerStream: ss}
err := handler(srv, dss)
rpcErr := handler(srv, dss)
var rpcStatus *internal.Status
if err != nil {
grpcStatus, _ := status.FromError(err)
if rpcErr != nil {
grpcStatus, _ := status.FromError(rpcErr)
rpcStatus = &internal.Status{
Code: grpcStatus.Code().String(),
Message: grpcStatus.Message(),
Expand All @@ -36,13 +37,16 @@ func dumpInterceptor(output io.Writer, decoder proto_decoder.MessageDecoder) grp
Metadata: md,
}

var err error
for _, message := range rpc.Messages {
message.Message, err = decoder.Decode(info.FullMethod, message)
// TODO: log warning if error occurs here
if err != nil {
logger.WithError(err).Warn("Failed to decode message")
}
}

dump, _ := json.Marshal(rpc)
fmt.Fprintln(output, string(dump))
return err
return rpcErr
}
}

0 comments on commit a0a8a9e

Please sign in to comment.