Skip to content

Commit

Permalink
Improve error message
Browse files Browse the repository at this point in the history
Improve error message for when input cannot be parsed. This is
in response to strange error messages resulting from binary proto being
sent to pb when json was expected.
  • Loading branch information
juliaogris committed Feb 21, 2022
1 parent f19888f commit e41cc98
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions cmd/pb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func run(cfg PBConfig) error {
}
unmarshal, err := cfg.unmarshaler()
if err != nil {
return err
return fmt.Errorf("cannot decode %q input: %w", cfg.inFormat(), err)
}
message := dynamicpb.NewMessage(md)
if err := unmarshal(in, message); err != nil {
Expand Down Expand Up @@ -106,8 +106,7 @@ func (c *PBConfig) writeOutput(b []byte) error {
}

func (c *PBConfig) unmarshaler() (unmarshaler, error) {
format := getFormat(c.In, c.InFormat)
switch format {
switch c.inFormat() {
case "json":
o := protojson.UnmarshalOptions{Resolver: c.Protoset}
return o.Unmarshal, nil
Expand All @@ -118,7 +117,11 @@ func (c *PBConfig) unmarshaler() (unmarshaler, error) {
o := prototext.UnmarshalOptions{Resolver: c.Protoset}
return o.Unmarshal, nil
}
return nil, fmt.Errorf("unknown input format %s", format)
return nil, fmt.Errorf("unknown input format %q", c.inFormat())
}

func (c *PBConfig) inFormat() string {
return getFormat(c.In, c.InFormat)
}

func (c *PBConfig) marshaler() (marshaler, error) {
Expand Down

0 comments on commit e41cc98

Please sign in to comment.