github.com/windmeup/goreleaser@v1.21.95/internal/middleware/errhandler/error.go (about)

     1  package errhandler
     2  
     3  import (
     4  	"github.com/caarlos0/log"
     5  	"github.com/hashicorp/go-multierror"
     6  	"github.com/windmeup/goreleaser/internal/middleware"
     7  	"github.com/windmeup/goreleaser/internal/pipe"
     8  	"github.com/windmeup/goreleaser/pkg/context"
     9  )
    10  
    11  // Handle handles an action error, ignoring and logging pipe skipped
    12  // errors.
    13  func Handle(action middleware.Action) middleware.Action {
    14  	return func(ctx *context.Context) error {
    15  		err := action(ctx)
    16  		if err == nil {
    17  			return nil
    18  		}
    19  		if pipe.IsSkip(err) {
    20  			log.WithField("reason", err.Error()).Warn("pipe skipped")
    21  			return nil
    22  		}
    23  		return err
    24  	}
    25  }
    26  
    27  // Memo is a handler that memorizes errors, so you can grab them all in the end
    28  // instead of returning each of them.
    29  type Memo struct {
    30  	err error
    31  }
    32  
    33  // Error returns the underlying error.
    34  func (m *Memo) Error() error {
    35  	return m.err
    36  }
    37  
    38  // Wrap the given action, memorizing its errors.
    39  // The resulting action will always return a nil error.
    40  func (m *Memo) Wrap(action middleware.Action) middleware.Action {
    41  	return func(ctx *context.Context) error {
    42  		err := action(ctx)
    43  		if err == nil {
    44  			return nil
    45  		}
    46  		m.Memorize(err)
    47  		return nil
    48  	}
    49  }
    50  
    51  func (m *Memo) Memorize(err error) {
    52  	if pipe.IsSkip(err) {
    53  		log.WithField("reason", err.Error()).Warn("pipe skipped")
    54  		return
    55  	}
    56  	m.err = multierror.Append(m.err, err)
    57  }