From 73452f441e3f56097eed73c1d73c8335d5a42b43 Mon Sep 17 00:00:00 2001 From: Akshay Shah Date: Tue, 3 Oct 2023 13:53:30 -0700 Subject: [PATCH] Move error strings into codecs Following https://github.com/connectrpc/connect-go/pull/599, make the errors returned by this package's codecs include the protobuf type name. --- binary.go | 12 ++++++++++-- error.go | 2 +- json.go | 6 +++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/binary.go b/binary.go index 10c15bd..8a7cab3 100644 --- a/binary.go +++ b/binary.go @@ -1,6 +1,8 @@ package connectproto import ( + "fmt" + "connectrpc.com/connect" "google.golang.org/protobuf/proto" ) @@ -49,7 +51,10 @@ func (b *binaryCodec) Unmarshal(binary []byte, msg any) error { if !ok { return errNotProto(msg) } - return b.unmarshal.Unmarshal(binary, pm) + if err := b.unmarshal.Unmarshal(binary, pm); err != nil { + return fmt.Errorf("unmarshal into %T: %w", pm, err) + } + return nil } func (b *binaryCodec) Marshal(msg any) ([]byte, error) { @@ -85,7 +90,10 @@ func newBinaryVTCodec() *vtBinaryCodec { func (v *vtBinaryCodec) Unmarshal(binary []byte, msg any) error { if vt, ok := msg.(interface{ UnmarshalVT([]byte) error }); ok { - return vt.UnmarshalVT(binary) + if err := vt.UnmarshalVT(binary); err != nil { + return fmt.Errorf("unmarshal into %T: %w", msg, err) + } + return nil } return v.binaryCodec.Unmarshal(binary, msg) } diff --git a/error.go b/error.go index dc2596d..a7da251 100644 --- a/error.go +++ b/error.go @@ -8,7 +8,7 @@ import ( func errNotProto(msg any) error { if _, ok := msg.(protoiface.MessageV1); ok { - return fmt.Errorf("%T uses github.com/golang/protobuf, but connect-go only supports google.golang.org/protobuf: see https://go.dev/blog/protobuf-apiv2", msg) + return fmt.Errorf("%T uses github.com/golang/protobuf, but connectproto only supports google.golang.org/protobuf: see https://go.dev/blog/protobuf-apiv2", msg) } return fmt.Errorf("%T doesn't implement proto.Message", msg) } diff --git a/json.go b/json.go index 8b343e8..c6d33f6 100644 --- a/json.go +++ b/json.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "errors" + "fmt" "connectrpc.com/connect" "google.golang.org/protobuf/encoding/protojson" @@ -36,7 +37,10 @@ func (j *jsonCodec) Unmarshal(binary []byte, msg any) error { if len(binary) == 0 { return errors.New("zero-length payload is not a valid JSON object") } - return j.unmarshal.Unmarshal(binary, pm) + if err := j.unmarshal.Unmarshal(binary, pm); err != nil { + return fmt.Errorf("unmarshal into %T: %w", pm, err) + } + return nil } func (j *jsonCodec) Marshal(msg any) ([]byte, error) {