37 lines
909 B
Go
37 lines
909 B
Go
|
package tools
|
||
|
|
||
|
import "fmt"
|
||
|
|
||
|
// Interface for all types of wrapper around a channel of results and an error channel
|
||
|
// Implementors will expose a type-specific channel for results
|
||
|
// Call the Wait() function after processing the results channel to catch any errors
|
||
|
// that occurred during the async processing
|
||
|
type ChannelWrapper interface {
|
||
|
// Call this after processing results channel to check for async errors
|
||
|
Wait() error
|
||
|
}
|
||
|
|
||
|
// Base implementation of channel wrapper to just deal with errors
|
||
|
type BaseChannelWrapper struct {
|
||
|
errorChan <-chan error
|
||
|
}
|
||
|
|
||
|
func (w *BaseChannelWrapper) Wait() error {
|
||
|
var err error
|
||
|
for e := range w.errorChan {
|
||
|
if err != nil {
|
||
|
// Combine in case multiple errors
|
||
|
err = fmt.Errorf("%v\n%v", err, e)
|
||
|
|
||
|
} else {
|
||
|
err = e
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func NewBaseChannelWrapper(errChan <-chan error) *BaseChannelWrapper {
|
||
|
return &BaseChannelWrapper{errorChan: errChan}
|
||
|
}
|