Skip to content

Commit

Permalink
It's safe to ignore the error from bytes.Buffer.Write methods.
Browse files Browse the repository at this point in the history
They can't actually error, they have an error return so that they fit the io.Writer interface. (https://golang.org/src/bytes/buffer.go#L170)

With the error removed the FIXME/panic in the caller should be safe to remove as well.
  • Loading branch information
Zachary Scott committed Aug 31, 2018
1 parent a2111dd commit c38515e
Showing 1 changed file with 9 additions and 23 deletions.
32 changes: 9 additions & 23 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,41 +88,27 @@ type Orb struct {
Executors map[string]struct{}
}

func addOrbElementsToBuffer(buf *bytes.Buffer, name string, elems map[string]struct{}) error {
func addOrbElementsToBuffer(buf *bytes.Buffer, name string, elems map[string]struct{}) {
var err error

if len(elems) > 0 {
_, err = buf.WriteString(fmt.Sprintf(" %s:\n", name))
if err != nil {
return err
}
for key := range elems {
_, err = buf.WriteString(fmt.Sprintf(" - %s\n", key))
if err != nil {
return err
}
}
}

return err
// This should never occur, but the linter made me do it :shrug:
if err != nil {
panic(err)
}
}

func (orb Orb) String() string {
var buffer bytes.Buffer

err := addOrbElementsToBuffer(&buffer, "Commands", orb.Commands)
// FIXME: refactor this to handle the error
if err != nil {
panic(err)
}
err = addOrbElementsToBuffer(&buffer, "Jobs", orb.Jobs)
if err != nil {
panic(err)
}
err = addOrbElementsToBuffer(&buffer, "Executors", orb.Executors)
if err != nil {
panic(err)
}
addOrbElementsToBuffer(&buffer, "Commands", orb.Commands)
addOrbElementsToBuffer(&buffer, "Jobs", orb.Jobs)
addOrbElementsToBuffer(&buffer, "Executors", orb.Executors)

return buffer.String()
}

Expand Down

0 comments on commit c38515e

Please sign in to comment.