From 72f35786a35a089396a8e3f4142a9835784acac1 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 1/2] 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 4809fe3c..7cd4552a 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 e87365e3..a7006f03 100644 --- a/protocol_connect.go +++ b/protocol_connect.go @@ -1099,7 +1099,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 } From 50535eaab51059d16159669ea550d7b639eb89aa Mon Sep 17 00:00:00 2001 From: Josh Humphries <2035234+jhump@users.noreply.github.com> Date: Tue, 3 Oct 2023 16:05:28 -0400 Subject: [PATCH 2/2] consistent error context; also fix type leak in envelopeReader (missed in first commit) --- envelope.go | 2 +- protocol_connect.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/envelope.go b/envelope.go index 5f02acfa..c207e096 100644 --- a/envelope.go +++ b/envelope.go @@ -219,7 +219,7 @@ func (r *envelopeReader) Unmarshal(message any) *Error { } if err := r.codec.Unmarshal(data.Bytes(), message); err != nil { - return errorf(CodeInvalidArgument, "unmarshal into %T: %w", message, err) + return errorf(CodeInvalidArgument, "unmarshal message: %w", err) } return nil } diff --git a/protocol_connect.go b/protocol_connect.go index a7006f03..b14eb4db 100644 --- a/protocol_connect.go +++ b/protocol_connect.go @@ -1099,7 +1099,7 @@ func (u *connectUnaryUnmarshaler) UnmarshalFunc(message any, unmarshal func([]by data = decompressed } if err := unmarshal(data.Bytes(), message); err != nil { - return NewError(CodeInvalidArgument, err) + return errorf(CodeInvalidArgument, "unmarshal message: %w", err) } return nil }