diff --git a/internal/impl/codec_field.go b/internal/impl/codec_field.go index 35d40029c..cb4b482d1 100644 --- a/internal/impl/codec_field.go +++ b/internal/impl/codec_field.go @@ -10,6 +10,7 @@ import ( "sync" "google.golang.org/protobuf/encoding/protowire" + "google.golang.org/protobuf/internal/errors" "google.golang.org/protobuf/proto" pref "google.golang.org/protobuf/reflect/protoreflect" preg "google.golang.org/protobuf/reflect/protoregistry" @@ -20,6 +21,7 @@ type errInvalidUTF8 struct{} func (errInvalidUTF8) Error() string { return "string field contains invalid UTF-8" } func (errInvalidUTF8) InvalidUTF8() bool { return true } +func (errInvalidUTF8) Unwrap() error { return errors.Error } // initOneofFieldCoders initializes the fast-path functions for the fields in a oneof. // diff --git a/proto/decode_test.go b/proto/decode_test.go index 3f64dd433..1b2f2166b 100644 --- a/proto/decode_test.go +++ b/proto/decode_test.go @@ -15,6 +15,7 @@ import ( "google.golang.org/protobuf/reflect/protoreflect" "google.golang.org/protobuf/testing/protopack" + "google.golang.org/protobuf/internal/errors" testpb "google.golang.org/protobuf/internal/testprotos/test" test3pb "google.golang.org/protobuf/internal/testprotos/test3" ) @@ -82,6 +83,8 @@ func TestDecodeInvalidMessages(t *testing.T) { got := want.ProtoReflect().New().Interface() if err := opts.Unmarshal(test.wire, got); err == nil { t.Errorf("Unmarshal unexpectedly succeeded\ninput bytes: [%x]\nMessage:\n%v", test.wire, prototext.Format(got)) + } else if !errors.Is(err, proto.Error) { + t.Errorf("Unmarshal error is not a proto.Error: %v", err) } }) } diff --git a/proto/encode_test.go b/proto/encode_test.go index 3915c9d74..967def78e 100644 --- a/proto/encode_test.go +++ b/proto/encode_test.go @@ -18,6 +18,7 @@ import ( "google.golang.org/protobuf/proto" pref "google.golang.org/protobuf/reflect/protoreflect" + "google.golang.org/protobuf/internal/errors" orderpb "google.golang.org/protobuf/internal/testprotos/order" testpb "google.golang.org/protobuf/internal/testprotos/test" test3pb "google.golang.org/protobuf/internal/testprotos/test3" @@ -136,6 +137,9 @@ func TestEncodeInvalidMessages(t *testing.T) { if err == nil { t.Fatalf("Marshal unexpectedly succeeded\noutput bytes: [%x]\nMessage:\n%v", got, prototext.Format(m)) } + if !errors.Is(err, proto.Error) { + t.Fatalf("Marshal error is not a proto.Error: %v", err) + } }) } }