Skip to content

Commit

Permalink
Move error strings into codecs
Browse files Browse the repository at this point in the history
Following connectrpc/connect-go#599, make the
errors returned by this package's codecs include the protobuf type name.
  • Loading branch information
akshayjshah committed Oct 3, 2023
1 parent cffcefe commit 73452f4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
12 changes: 10 additions & 2 deletions binary.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package connectproto

import (
"fmt"

"connectrpc.com/connect"
"google.golang.org/protobuf/proto"
)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
6 changes: 5 additions & 1 deletion json.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"errors"
"fmt"

"connectrpc.com/connect"
"google.golang.org/protobuf/encoding/protojson"
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 73452f4

Please sign in to comment.