Skip to content

Commit

Permalink
Fix issues with error details (#379)
Browse files Browse the repository at this point in the history
Co-authored-by: Erik Engberg <[email protected]>
  • Loading branch information
fdfzcq and erikjoh authored Jun 22, 2023
1 parent d5b8e4d commit 3961a33
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions format.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,25 @@ type Formatter func(proto.Message) (string, error)
func NewJSONFormatter(emitDefaults bool, resolver jsonpb.AnyResolver) Formatter {
marshaler := jsonpb.Marshaler{
EmitDefaults: emitDefaults,
Indent: " ",
AnyResolver: resolver,
}
return marshaler.MarshalToString
// Workaround for indentation issue in jsonpb with Any messages.
// Bug was originally fixed in https://github.com/golang/protobuf/pull/834
// but later re-introduced before the module was deprecated and frozen.
// If jsonpb is ever replaced with google.golang.org/protobuf/encoding/protojson
// this workaround will no longer be needed.
formatter := func(message proto.Message) (string, error) {
output, err := marshaler.MarshalToString(message)
if err != nil {
return "", err
}
var buf bytes.Buffer
if err := json.Indent(&buf, []byte(output), "", " "); err != nil {
return "", err
}
return buf.String(), nil
}
return formatter
}

// NewTextFormatter returns a formatter that returns strings in the protobuf
Expand Down Expand Up @@ -274,11 +289,11 @@ func (r *anyResolver) Resolve(typeUrl string) (proto.Message, error) {
if !ok {
return nil, fmt.Errorf("unknown message: %s", typeUrl)
}
// populate any extensions for this message, too
if exts, err := r.source.AllExtensionsForType(mname); err != nil {
return nil, err
} else if err := r.er.AddExtension(exts...); err != nil {
return nil, err
// populate any extensions for this message, too (if there are any)
if exts, err := r.source.AllExtensionsForType(mname); err == nil {
if err := r.er.AddExtension(exts...); err != nil {
return nil, err
}
}

if r.mf == nil {
Expand Down

0 comments on commit 3961a33

Please sign in to comment.