From a0f95d5b14735d688bd94ca511d396115e5b86be Mon Sep 17 00:00:00 2001 From: Damien Neil Date: Fri, 24 Jul 2020 14:25:43 -0700 Subject: [PATCH] internal/impl: make errInvalidUTF8 be a proto.Error Fixes golang/protobuf#1174. Change-Id: If95f9f79fff76dc0c44f7ee96e484f59fa287e58 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/244718 Reviewed-by: Joe Tsai --- internal/impl/codec_field.go | 2 ++ proto/decode_test.go | 3 +++ proto/encode_test.go | 4 ++++ 3 files changed, 9 insertions(+) diff --git a/internal/impl/codec_field.go b/internal/impl/codec_field.go index 35d40029..cb4b482d 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 3f64dd43..1b2f2166 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 3915c9d7..967def78 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) + } }) } }