multierror and multilogger cleaned up.

This commit is contained in:
2022-01-05 16:36:20 -05:00
parent 4addc44c0f
commit ef0a4d825d
3 changed files with 59 additions and 15 deletions

View File

@@ -25,7 +25,7 @@ Example:
}
if errs != nil && len(errs) != 0 {
// err now contains multiple errors presented as a single error.
// err now contains multiple errors presented as a single error interface.
err = multierr.NewErrors(errs...)
}
}
@@ -50,7 +50,12 @@ MultiError also has a shorthand, making the above much less verbose:
}
}()
}
// multierror now contains any/all errors above.
// 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.

View File

@@ -39,12 +39,19 @@ func NewErrors(errs ...error) (err error) {
// NewMultiError will provide a MultiError (true type), optionally initialized with errors.
func NewMultiError(errs ...error) (m *MultiError) {
if errs == nil {
errs = make([]error, 0)
var realErrs []error = make([]error, 0)
if errs != nil {
for _, e := range errs {
if e == nil {
continue
}
realErrs = append(realErrs, e)
}
}
m = &MultiError{
Errors: errs,
Errors: realErrs,
ErrorSep: "\n",
}
@@ -83,3 +90,21 @@ func (e *MultiError) AddError(err error) {
e.Errors = append(e.Errors, err)
}
// Count returns the number of errors in a MultiError.
func (e *MultiError) Count() (n int) {
n = len(e.Errors)
return
}
// IsEmpty is a shorthand for testing if e.Errors is empty.
func (e *MultiError) IsEmpty() (empty bool) {
if e.Count() == 0 {
empty = true
}
return
}