go-openssl/utils/errors.go
Jeff Wendling fa8eb6a573 space monkey internal commit export
[katamari commit: 9bd04d1d78e85304589695c66e328d23128f509c]
2014-02-25 19:01:49 +00:00

39 lines
885 B
Go

// Copyright (C) 2014 Space Monkey, Inc.
package utils
import (
"errors"
"strings"
)
// ErrorGroup collates errors
type ErrorGroup struct {
Errors []error
}
// Add adds an error to an existing error group
func (e *ErrorGroup) Add(err error) {
if err != nil {
e.Errors = append(e.Errors, err)
}
}
// Finalize returns an error corresponding to the ErrorGroup state. If there's
// no errors in the group, finalize returns nil. If there's only one error,
// Finalize returns that error. Otherwise, Finalize will make a new error
// consisting of the messages from the constituent errors.
func (e *ErrorGroup) Finalize() error {
if len(e.Errors) == 0 {
return nil
}
if len(e.Errors) == 1 {
return e.Errors[0]
}
msgs := make([]string, 0, len(e.Errors))
for _, err := range e.Errors {
msgs = append(msgs, err.Error())
}
return errors.New(strings.Join(msgs, "\n"))
}