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 <joetsai@google.com>
diff --git a/internal/impl/codec_field.go b/internal/impl/codec_field.go
index 35d4002..cb4b482 100644
--- a/internal/impl/codec_field.go
+++ b/internal/impl/codec_field.go
@@ -10,6 +10,7 @@
"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 @@
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 3f64dd4..1b2f216 100644
--- a/proto/decode_test.go
+++ b/proto/decode_test.go
@@ -15,6 +15,7 @@
"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 @@
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 3915c9d..967def7 100644
--- a/proto/encode_test.go
+++ b/proto/encode_test.go
@@ -18,6 +18,7 @@
"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 @@
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)
+ }
})
}
}