Skip to content

Commit

Permalink
proto: add invalid JSON to JSONBMarshalToMessage error message
Browse files Browse the repository at this point in the history
This change makes debugging easier by including the invalid JSON
in that caused JSONBMarshalToMessage to fail.

Release note: None
  • Loading branch information
ecwall committed Aug 1, 2023
1 parent 509f8c8 commit 08d18aa
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkg/sql/logictest/testdata/logic_test/builtin_function
Original file line number Diff line number Diff line change
Expand Up @@ -2989,7 +2989,7 @@ select crdb_internal.json_to_pb('cockroach.sql.sqlbase.Descriptor', crdb_interna
----
true

query error pq: crdb_internal.json_to_pb\(\): invalid proto JSON: unmarshaling json to cockroach.sql.sqlbase.Descriptor: unknown field "__redacted__" in descpb.Descriptor
query error pq: crdb_internal.json_to_pb\(\): invalid proto JSON: unmarshaling to cockroach.sql.sqlbase.Descriptor json: .+ unknown field "__redacted__" in descpb.Descriptor
select crdb_internal.json_to_pb('cockroach.sql.sqlbase.Descriptor', crdb_internal.pb_to_json('cockroach.sql.sqlbase.Descriptor', descriptor, true, true)) = descriptor from system.descriptor where id = 1

subtest regexp_split
Expand Down
5 changes: 3 additions & 2 deletions pkg/sql/protoreflect/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ func MessageToJSON(msg protoutil.Message, flags FmtFlags) (jsonb.JSON, error) {
// Returns serialized byte representation of the protocol message.
func JSONBMarshalToMessage(input jsonb.JSON, target protoutil.Message) ([]byte, error) {
json := &jsonpb.Unmarshaler{}
if err := json.Unmarshal(strings.NewReader(input.String()), target); err != nil {
return nil, errors.Wrapf(err, "unmarshaling json to %s", proto.MessageName(target))
jsonString := input.String()
if err := json.Unmarshal(strings.NewReader(jsonString), target); err != nil {
return nil, errors.Wrapf(err, "unmarshaling to %s json: %s", proto.MessageName(target), jsonString)
}
data, err := protoutil.Marshal(target)
if err != nil {
Expand Down

0 comments on commit 08d18aa

Please sign in to comment.