mirror of
https://github.com/libp2p/go-openssl.git
synced 2025-01-15 02:40:09 +08:00
fa8eb6a573
[katamari commit: 9bd04d1d78e85304589695c66e328d23128f509c]
39 lines
885 B
Go
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"))
|
|
}
|