grpcerrors: use full typeurl registration

Also switches current types to json.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
v0.8
Tonis Tiigi 2020-07-26 20:04:47 -07:00
parent 6d14bfd206
commit 862387445e
4 changed files with 38 additions and 17 deletions

1
go.mod
View File

@ -17,6 +17,7 @@ require (
github.com/containerd/fifo v0.0.0-20200410184934-f15a3290365b // indirect github.com/containerd/fifo v0.0.0-20200410184934-f15a3290365b // indirect
github.com/containerd/go-cni v1.0.0 github.com/containerd/go-cni v1.0.0
github.com/containerd/go-runc v0.0.0-20200220073739-7016d3ce2328 github.com/containerd/go-runc v0.0.0-20200220073739-7016d3ce2328
github.com/containerd/typeurl v1.0.1
github.com/coreos/go-systemd/v22 v22.1.0 github.com/coreos/go-systemd/v22 v22.1.0
github.com/docker/cli v0.0.0-20200227165822-2298e6a3fe24 github.com/docker/cli v0.0.0-20200227165822-2298e6a3fe24
github.com/docker/distribution v2.7.1+incompatible github.com/docker/distribution v2.7.1+incompatible

View File

@ -1,14 +1,14 @@
package errdefs package errdefs
import ( import (
proto "github.com/golang/protobuf/proto" "github.com/containerd/typeurl"
"github.com/moby/buildkit/util/grpcerrors" "github.com/moby/buildkit/util/grpcerrors"
digest "github.com/opencontainers/go-digest" digest "github.com/opencontainers/go-digest"
) )
func init() { func init() {
proto.RegisterType((*Vertex)(nil), "errdefs.Vertex") typeurl.Register((*Vertex)(nil), "github.com/moby/buildkit", "errdefs.Vertex+json")
proto.RegisterType((*Source)(nil), "errdefs.Source") typeurl.Register((*Source)(nil), "github.com/moby/buildkit", "errdefs.Source+json")
} }
type VertexError struct { type VertexError struct {

View File

@ -1,11 +1,15 @@
package grpcerrors package grpcerrors
import ( import (
"encoding/json"
"errors"
"github.com/containerd/typeurl"
gogotypes "github.com/gogo/protobuf/types" gogotypes "github.com/gogo/protobuf/types"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/any" "github.com/golang/protobuf/ptypes/any"
"github.com/moby/buildkit/util/stack" "github.com/moby/buildkit/util/stack"
"github.com/sirupsen/logrus"
spb "google.golang.org/genproto/googleapis/rpc/status" spb "google.golang.org/genproto/googleapis/rpc/status"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
@ -47,7 +51,7 @@ func ToGRPC(err error) error {
}) })
if len(details) > 0 { if len(details) > 0 {
if st2, err := st.WithDetails(details...); err == nil { if st2, err := withDetails(st, details...); err == nil {
st = st2 st = st2
} }
} }
@ -55,6 +59,26 @@ func ToGRPC(err error) error {
return st.Err() return st.Err()
} }
func withDetails(s *status.Status, details ...proto.Message) (*status.Status, error) {
if s.Code() == codes.OK {
return nil, errors.New("no error details for status with code OK")
}
p := s.Proto()
for _, detail := range details {
url, err := typeurl.TypeURL(detail)
if err != nil {
logrus.Warnf("ignoring typed error %T: not registered", detail)
continue
}
dt, err := json.Marshal(detail)
if err != nil {
return nil, err
}
p.Details = append(p.Details, &any.Any{TypeUrl: url, Value: dt})
}
return status.FromProto(p), nil
}
func Code(err error) codes.Code { func Code(err error) codes.Code {
if se, ok := err.(interface { if se, ok := err.(interface {
Code() codes.Code Code() codes.Code
@ -123,17 +147,9 @@ func FromGRPC(err error) error {
// details that we don't understand are copied as proto // details that we don't understand are copied as proto
for _, d := range pb.Details { for _, d := range pb.Details {
var m interface{} m, err := typeurl.UnmarshalAny(gogoAny(d))
detail := &ptypes.DynamicAny{} if err != nil {
if err := ptypes.UnmarshalAny(d, detail); err != nil { continue
detail := &gogotypes.DynamicAny{}
if err := gogotypes.UnmarshalAny(gogoAny(d), detail); err != nil {
n.Details = append(n.Details, d)
continue
}
m = detail.Message
} else {
m = detail.Message
} }
switch v := m.(type) { switch v := m.(type) {
@ -144,7 +160,6 @@ func FromGRPC(err error) error {
default: default:
n.Details = append(n.Details, d) n.Details = append(n.Details, d)
} }
} }
err = status.FromProto(n).Err() err = status.FromProto(n).Err()

View File

@ -7,9 +7,14 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/containerd/typeurl"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
func init() {
typeurl.Register((*Stack)(nil), "github.com/moby/buildkit", "stack.Stack+json")
}
var version string var version string
var revision string var revision string