chore: add lock for batcherror (#3950)

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

Loading…
Cancel
Save