github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/fvm/errors/errors_collector.go (about) 1 package errors 2 3 import ( 4 "fmt" 5 6 "github.com/hashicorp/go-multierror" 7 ) 8 9 // Utility object for collecting Processor errors. 10 type ErrorsCollector struct { 11 errors error 12 failure error 13 } 14 15 func NewErrorsCollector() *ErrorsCollector { 16 return &ErrorsCollector{} 17 } 18 19 func (collector *ErrorsCollector) CollectedFailure() bool { 20 return collector.failure != nil 21 } 22 23 func (collector *ErrorsCollector) CollectedError() bool { 24 return collector.errors != nil || collector.failure != nil 25 } 26 27 func (collector *ErrorsCollector) ErrorOrNil() error { 28 if collector.failure == nil { 29 return collector.errors 30 } 31 32 if collector.errors == nil { 33 return collector.failure 34 } 35 36 return fmt.Errorf( 37 "%w (additional errors: %v)", 38 collector.failure, 39 collector.errors) 40 } 41 42 // This returns collector for chaining purposes. 43 func (collector *ErrorsCollector) Collect(err error) *ErrorsCollector { 44 if err == nil { 45 return collector 46 } 47 48 if collector.failure != nil { 49 collector.errors = multierror.Append(collector.errors, err) 50 return collector 51 } 52 53 if IsFailure(err) { 54 collector.failure = err 55 return collector 56 } 57 58 collector.errors = multierror.Append(collector.errors, err) 59 return collector 60 }