Documentation
¶
Overview ¶
Package multierr provides a simple way of handling multiple errors and consolidating them into a single error.
Example:
package main
import (
`r00t2.io/goutils/multierr`
)
func main() {
var err error
var errs []error
errs = make([]error, 0)
for _, i := range someSlice {
go func() {
if err = i.DoSomething(); err != nil {
errs = append(errs, err)
}
}()
}
if errs != nil && len(errs) != 0 {
// err now contains multiple errors presented as a single error interface.
err = multierr.NewErrors(errs...)
}
}
MultiError also has a shorthand, making the above much less verbose:
package main
import (
`r00t2.io/goutils/multierr`
)
func main() {
var err error
var multierror *multierr.MultiError = multierr.NewMultiError(nil)
for _, i := range someSlice {
go func() {
if err = i.DoSomething(); err != nil {
multierror.AddError(err)
}
}()
}
// multierror now contains any/all errors above. If calling in a function, you'll probably want to do:
// if !multierror.IsEmpty() {
// err = multierror
// }
}
In the above, the multierror assignment can still be used as an error.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type MultiError ¶
type MultiError struct {
// Errors is a slice of errors to combine/concatenate when .Error() is called.
Errors []error `json:"errors"`
// ErrorSep is a string to use to separate errors for .Error(). The default is "\n".
ErrorSep string `json:"separator"`
// contains filtered or unexported fields
}
MultiError is a type of error.Error that can contain multiple errors.
func NewMultiError ¶
func NewMultiError(errs ...error) (m *MultiError)
NewMultiError will provide a MultiError (true type), optionally initialized with errors.
func (*MultiError) AddError ¶
func (e *MultiError) AddError(err error)
AddError is a shorthand way of adding an error to a MultiError.
func (*MultiError) Count ¶
func (e *MultiError) Count() (n int)
Count returns the number of errors in a MultiError.
func (*MultiError) Error ¶
func (e *MultiError) Error() (errStr string)
Error returns a string representation of a MultiError (to conform with the error interface).
func (*MultiError) IsEmpty ¶
func (e *MultiError) IsEmpty() (empty bool)
IsEmpty is a shorthand for testing if e.Errors is empty.
Source Files
¶
- doc.go
- funcs.go
- types.go