Files
trufflehog/pkg/sources/errors.go
ahrav 8be89a593b Handle errors in a thread safe manner (#1052)
* Handle errors in a thread safe manner.

* fix test.

* fix linter.

* address comments.
2023-02-02 11:05:33 -08:00

32 lines
724 B
Go

package sources
import (
"sync"
)
// ScanErrors is used to collect errors encountered while scanning.
// It ensures that errors are collected in a thread-safe manner.
type ScanErrors struct {
mu sync.RWMutex
errors []error
}
// NewScanErrors creates a new thread safe error collector.
func NewScanErrors(projects int) *ScanErrors {
return &ScanErrors{errors: make([]error, 0, projects)}
}
// Add an error to the collection in a thread-safe manner.
func (s *ScanErrors) Add(err error) {
s.mu.Lock()
defer s.mu.Unlock()
s.errors = append(s.errors, err)
}
// Count returns the number of errors collected.
func (s *ScanErrors) Count() uint64 {
s.mu.RLock()
defer s.mu.RUnlock()
return uint64(len(s.errors))
}