2021-09-18 10:21:29 -03:00
|
|
|
package errhandler
|
2019-01-22 01:56:16 -02:00
|
|
|
|
|
|
|
import (
|
2022-06-21 21:11:15 -03:00
|
|
|
"github.com/caarlos0/log"
|
2021-09-18 10:21:29 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/middleware"
|
2019-01-22 01:56:16 -02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/pipe"
|
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
2022-12-28 12:24:21 -03:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2019-01-22 01:56:16 -02:00
|
|
|
)
|
|
|
|
|
2021-09-18 10:21:29 -03:00
|
|
|
// Handle handles an action error, ignoring and logging pipe skipped
|
2019-01-22 01:56:16 -02:00
|
|
|
// errors.
|
2021-09-18 10:21:29 -03:00
|
|
|
func Handle(action middleware.Action) middleware.Action {
|
2019-01-22 01:56:16 -02:00
|
|
|
return func(ctx *context.Context) error {
|
2021-04-19 09:31:57 -03:00
|
|
|
err := action(ctx)
|
2019-01-22 01:56:16 -02:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if pipe.IsSkip(err) {
|
2022-07-04 21:12:56 -03:00
|
|
|
log.WithField("reason", err.Error()).Warn("pipe skipped")
|
2019-01-22 01:56:16 -02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-12-28 12:24:21 -03:00
|
|
|
|
|
|
|
// Memo is a handler that memorizes errors, so you can grab them all in the end
|
|
|
|
// instead of returning each of them.
|
|
|
|
type Memo struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error returns the underlying error.
|
|
|
|
func (m *Memo) Error() error {
|
|
|
|
return m.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap the given action, memorizing its errors.
|
|
|
|
// The resulting action will always return a nil error.
|
|
|
|
func (m *Memo) Wrap(action middleware.Action) middleware.Action {
|
|
|
|
return func(ctx *context.Context) error {
|
|
|
|
err := action(ctx)
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2023-06-20 09:33:59 -03:00
|
|
|
m.Memorize(err)
|
2022-12-28 12:24:21 -03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2023-06-20 09:33:59 -03:00
|
|
|
|
|
|
|
func (m *Memo) Memorize(err error) {
|
|
|
|
if pipe.IsSkip(err) {
|
|
|
|
log.WithField("reason", err.Error()).Warn("pipe skipped")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m.err = multierror.Append(m.err, err)
|
|
|
|
}
|