trufflehog/pkg/sources/errors.go
Miccah a07b6664f8
Support fatal errors in job reports (#1562)
* Support fatal errors in job reports

* WIP: JobReporter and JobInspector

* WIP: JobReportHook and JobReportRef

* Add ChunkError type and asyncRun helper method

* Rename JobReport to JobProgress

* Return a closed channel from Done when the JobProgress is nil

* Comment catchFirstFatal function
2023-07-31 11:28:30 -05:00

45 lines
943 B
Go

package sources
import (
"errors"
"fmt"
"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() *ScanErrors {
return &ScanErrors{errors: make([]error, 0)}
}
// 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))
}
func (s *ScanErrors) String() string {
s.mu.RLock()
defer s.mu.RUnlock()
return fmt.Sprintf("%v", s.errors)
}
func (s *ScanErrors) Errors() error {
s.mu.RLock()
defer s.mu.RUnlock()
return errors.Join(s.errors...)
}