48 lines
714 B
Go
48 lines
714 B
Go
package custom_errors
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
type JobError struct {
|
|
ShouldCancelJob bool
|
|
Msg string
|
|
Prev error
|
|
}
|
|
|
|
func (e *JobError) Error() string {
|
|
if e.Prev != nil {
|
|
return fmt.Sprintf("%s: %v", e.Msg, e.Prev)
|
|
}
|
|
|
|
return e.Msg
|
|
}
|
|
|
|
func JobErrorHandler(ctx context.Context, chErrorsIn <-chan JobError) error {
|
|
for {
|
|
if ctx.Err() != nil {
|
|
return nil
|
|
}
|
|
|
|
select {
|
|
case <-ctx.Done():
|
|
return nil
|
|
|
|
case err, ok := <-chErrorsIn:
|
|
if !ok {
|
|
return nil
|
|
}
|
|
|
|
if err.ShouldCancelJob {
|
|
log.Errorf("(Fatal job error) - %v - %v", err.Msg, err.Prev)
|
|
return &err
|
|
}
|
|
|
|
log.Errorf("%v - %v", err.Msg, err.Prev)
|
|
}
|
|
}
|
|
}
|