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.Error(err.Msg, " - ", err.Prev) return &err } log.Error(err.Msg, " - ", err.Prev) } } }