finalizing logging and multierror

This commit is contained in:
2022-01-05 05:15:38 -05:00
parent 3975f8b11f
commit 0e01306637
24 changed files with 1057 additions and 60 deletions

58
multierr/doc.go Normal file
View File

@@ -0,0 +1,58 @@
/*
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.
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.
}
In the above, the multierror assignment can still be used as an error.
*/
package multierr

85
multierr/funcs.go Normal file
View File

@@ -0,0 +1,85 @@
package multierr
import (
`fmt`
)
/*
NewErrors returns a new MultiError (as an error) based on/initialized with a slice of error.Error (errs).
Any nil errors are trimmed.
If there are no actual errors after trimming, err will be nil.
*/
func NewErrors(errs ...error) (err error) {
if errs == nil || len(errs) == 0 {
return
}
var realErrs []error = make([]error, 0)
for _, e := range errs {
if e == nil {
continue
}
realErrs = append(realErrs, e)
}
if len(realErrs) == 0 {
return
}
err = &MultiError{
Errors: realErrs,
ErrorSep: "\n",
}
return
}
// NewMultiError will provide a MultiError (true type), optionally initialized with errors.
func NewMultiError(errs ...error) (m *MultiError) {
if errs == nil {
errs = make([]error, 0)
}
m = &MultiError{
Errors: errs,
ErrorSep: "\n",
}
return
}
// Error returns a string representation of a MultiError (to conform with the error interface).
func (e *MultiError) Error() (errStr string) {
var numErrs int
if e == nil || len(e.Errors) == 0 {
return
} else {
numErrs = len(e.Errors)
}
for idx, err := range e.Errors {
if (idx + 1) < numErrs {
errStr += fmt.Sprintf("%v%v", err.Error(), e.ErrorSep)
} else {
errStr += err.Error()
}
}
return
}
// AddError is a shorthand way of adding an error to a MultiError.
func (e *MultiError) AddError(err error) {
if err == nil {
return
}
e.Errors = append(e.Errors, err)
}

9
multierr/types.go Normal file
View File

@@ -0,0 +1,9 @@
package multierr
// MultiError is a type of error.Error that can contain multiple errors.
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"`
}