Skip to content

Commit

Permalink
error details: add @type key by switching to any.Any
Browse files Browse the repository at this point in the history
This is arguably more correct than what was introduced in grpc-ecosystem#515. So, any
error details no have a "@type" field indicating their underlying
proto.Message's type, following the Cloud API docs[1].

[1]: https://cloud.google.com/apis/design/errors#http_mapping

Signed-off-by: Stephan Renatus <[email protected]>
  • Loading branch information
srenatus committed Feb 21, 2018
1 parent 424b8e1 commit 915d69a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
7 changes: 7 additions & 0 deletions examples/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,13 @@ func TestErrorWithDetails(t *testing.T) {
if got, want := ok, true; got != want {
t.Fatalf("msg.Details[0] got type: %T, want %T", msg.Details[0], map[string]interface{}{})
}
typ, ok := details["@type"].(string)
if got, want := ok, true; got != want {
t.Fatalf("msg.Details[0][\"@type\"] got type: %T, want %T", typ, "")
}
if got, want := details["@type"], "type.googleapis.com/google.rpc.DebugInfo"; got != want {
t.Errorf("msg.Details[\"@type\"] = %q; want %q", got, want)
}
if got, want := details["detail"], "error debug details"; got != want {
t.Errorf("msg.Details[\"detail\"] = %q; want %q", got, want)
}
Expand Down
15 changes: 11 additions & 4 deletions runtime/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"net/http"

"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/any"
"golang.org/x/net/context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/grpclog"
Expand Down Expand Up @@ -63,9 +65,9 @@ var (
)

type errorBody struct {
Error string `protobuf:"bytes,1,name=error" json:"error"`
Code int32 `protobuf:"varint,2,name=code" json:"code"`
Details []proto.Message `protobuf:"bytes,3,name=details" json:"details"`
Error string `protobuf:"bytes,1,name=error" json:"error"`
Code int32 `protobuf:"varint,2,name=code" json:"code"`
Details []any.Any `protobuf:"bytes,3,name=details" json:"details"`
}

// Make this also conform to proto.Message for builtin JSONPb Marshaler
Expand Down Expand Up @@ -97,7 +99,12 @@ func DefaultHTTPError(ctx context.Context, mux *ServeMux, marshaler Marshaler, w

for _, detail := range s.Details() {
if det, ok := detail.(proto.Message); ok {
body.Details = append(body.Details, det)
a, err := ptypes.MarshalAny(det)
if err != nil {
grpclog.Printf("Failed to marshal any: %v", err)
} else {
body.Details = append(body.Details, *a)
}
}
}

Expand Down

0 comments on commit 915d69a

Please sign in to comment.