From e0d59e9a782c5b6789452b67e27b18d043e76ab7 Mon Sep 17 00:00:00 2001 From: Josh Humphries <2035234+jhump@users.noreply.github.com> Date: Mon, 18 Sep 2023 13:45:40 -0400 Subject: [PATCH] move the indication of destination message type into Codec, so a custom Codec could obscure it --- codec.go | 12 ++++++++++-- protocol_connect.go | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/codec.go b/codec.go index 4a624c59..bfb7226f 100644 --- a/codec.go +++ b/codec.go @@ -118,7 +118,11 @@ func (c *protoBinaryCodec) Unmarshal(data []byte, message any) error { if !ok { return errNotProto(message) } - return proto.Unmarshal(data, protoMessage) + err := proto.Unmarshal(data, protoMessage) + if err != nil { + return fmt.Errorf("unmarshal into %T: %w", message, err) + } + return nil } func (c *protoBinaryCodec) MarshalStable(message any) ([]byte, error) { @@ -174,7 +178,11 @@ func (c *protoJSONCodec) Unmarshal(binary []byte, message any) error { // Discard unknown fields so clients and servers aren't forced to always use // exactly the same version of the schema. options := protojson.UnmarshalOptions{DiscardUnknown: true} - return options.Unmarshal(binary, protoMessage) + err := options.Unmarshal(binary, protoMessage) + if err != nil { + return fmt.Errorf("unmarshal into %T: %w", message, err) + } + return nil } func (c *protoJSONCodec) MarshalStable(message any) ([]byte, error) { diff --git a/protocol_connect.go b/protocol_connect.go index 110666d3..62c6ed68 100644 --- a/protocol_connect.go +++ b/protocol_connect.go @@ -1101,7 +1101,7 @@ func (u *connectUnaryUnmarshaler) UnmarshalFunc(message any, unmarshal func([]by data = decompressed } if err := unmarshal(data.Bytes(), message); err != nil { - return errorf(CodeInvalidArgument, "unmarshal into %T: %w", message, err) + return NewError(CodeInvalidArgument, err) } return nil }