chore: add lock for batcherror (#3950)

master^2
Kevin Wan 8 months ago committed by GitHub
parent ec41880476
commit 03b6e377d7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -8,8 +8,8 @@ import (
type (
// A BatchError is an error that can hold multiple errors.
BatchError struct {
mu sync.Mutex
errs errorArray
lock sync.Mutex
}
errorArray []error
@ -17,8 +17,8 @@ type (
// Add adds errs to be, nil errors are ignored.
func (be *BatchError) Add(errs ...error) {
be.mu.Lock()
defer be.mu.Unlock()
be.lock.Lock()
defer be.lock.Unlock()
for _, err := range errs {
if err != nil {
@ -29,6 +29,9 @@ func (be *BatchError) Add(errs ...error) {
// Err returns an error that represents all errors.
func (be *BatchError) Err() error {
be.lock.Lock()
defer be.lock.Unlock()
switch len(be.errs) {
case 0:
return nil
@ -41,6 +44,9 @@ func (be *BatchError) Err() error {
// NotNil checks if any error inside.
func (be *BatchError) NotNil() bool {
be.lock.Lock()
defer be.lock.Unlock()
return len(be.errs) > 0
}

Loading…
Cancel
Save