multierror and multilogger cleaned up.

This commit is contained in:
brent s. 2022-01-05 16:36:20 -05:00
parent 4addc44c0f
commit ef0a4d825d
Signed by: bts
GPG Key ID: 8C004C2F93481F6B
3 changed files with 59 additions and 15 deletions

View File

@ -114,7 +114,9 @@ func (m *MultiLogger) Alert(s string, v ...interface{}) (err error) {


wg.Wait() wg.Wait()


err = e if !e.IsEmpty() {
err = e
}


return return
} }
@ -143,7 +145,9 @@ func (m *MultiLogger) Crit(s string, v ...interface{}) (err error) {


wg.Wait() wg.Wait()


err = e if !e.IsEmpty() {
err = e
}


return return
} }
@ -172,8 +176,9 @@ func (m *MultiLogger) Debug(s string, v ...interface{}) (err error) {


wg.Wait() wg.Wait()


err = e if !e.IsEmpty() {

err = e
}
return return
} }


@ -201,8 +206,9 @@ func (m *MultiLogger) Emerg(s string, v ...interface{}) (err error) {


wg.Wait() wg.Wait()


err = e if !e.IsEmpty() {

err = e
}
return return
} }


@ -230,7 +236,9 @@ func (m *MultiLogger) Err(s string, v ...interface{}) (err error) {


wg.Wait() wg.Wait()


err = e if !e.IsEmpty() {
err = e
}


return return
} }
@ -259,7 +267,9 @@ func (m *MultiLogger) Info(s string, v ...interface{}) (err error) {


wg.Wait() wg.Wait()


err = e if !e.IsEmpty() {
err = e
}


return return
} }
@ -288,7 +298,9 @@ func (m *MultiLogger) Notice(s string, v ...interface{}) (err error) {


wg.Wait() wg.Wait()


err = e if !e.IsEmpty() {
err = e
}


return return
} }
@ -317,7 +329,9 @@ func (m *MultiLogger) Warning(s string, v ...interface{}) (err error) {


wg.Wait() wg.Wait()


err = e if !e.IsEmpty() {
err = e
}


return return
} }

View File

@ -25,7 +25,7 @@ Example:
} }


if errs != nil && len(errs) != 0 { 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...) 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. 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. // NewMultiError will provide a MultiError (true type), optionally initialized with errors.
func NewMultiError(errs ...error) (m *MultiError) { func NewMultiError(errs ...error) (m *MultiError) {


if errs == nil { var realErrs []error = make([]error, 0)
errs = make([]error, 0)
if errs != nil {
for _, e := range errs {
if e == nil {
continue
}
realErrs = append(realErrs, e)
}
} }


m = &MultiError{ m = &MultiError{
Errors: errs, Errors: realErrs,
ErrorSep: "\n", ErrorSep: "\n",
} }


@ -83,3 +90,21 @@ func (e *MultiError) AddError(err error) {
e.Errors = append(e.Errors, err) 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
}