Fix issues with error details (#379)
Co-authored-by: Erik Engberg <ejohansson@spotify.com>
This commit is contained in:
parent
d5b8e4d4ce
commit
3961a33e7f
27
format.go
27
format.go
|
|
@ -136,10 +136,25 @@ type Formatter func(proto.Message) (string, error)
|
||||||
func NewJSONFormatter(emitDefaults bool, resolver jsonpb.AnyResolver) Formatter {
|
func NewJSONFormatter(emitDefaults bool, resolver jsonpb.AnyResolver) Formatter {
|
||||||
marshaler := jsonpb.Marshaler{
|
marshaler := jsonpb.Marshaler{
|
||||||
EmitDefaults: emitDefaults,
|
EmitDefaults: emitDefaults,
|
||||||
Indent: " ",
|
|
||||||
AnyResolver: resolver,
|
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
|
// NewTextFormatter returns a formatter that returns strings in the protobuf
|
||||||
|
|
@ -274,12 +289,12 @@ func (r *anyResolver) Resolve(typeUrl string) (proto.Message, error) {
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("unknown message: %s", typeUrl)
|
return nil, fmt.Errorf("unknown message: %s", typeUrl)
|
||||||
}
|
}
|
||||||
// populate any extensions for this message, too
|
// populate any extensions for this message, too (if there are any)
|
||||||
if exts, err := r.source.AllExtensionsForType(mname); err != nil {
|
if exts, err := r.source.AllExtensionsForType(mname); err == nil {
|
||||||
return nil, err
|
if err := r.er.AddExtension(exts...); err != nil {
|
||||||
} else if err := r.er.AddExtension(exts...); err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if r.mf == nil {
|
if r.mf == nil {
|
||||||
r.mf = dynamic.NewMessageFactoryWithExtensionRegistry(&r.er)
|
r.mf = dynamic.NewMessageFactoryWithExtensionRegistry(&r.er)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue