Skip to content

Commit

Permalink
Add ErrAbort type that can terminate a pipeline without error
Browse files Browse the repository at this point in the history
  • Loading branch information
ccremer committed Dec 20, 2021
1 parent b0333df commit 492492b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 7 deletions.
33 changes: 33 additions & 0 deletions examples/abort_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//go:build examples
// +build examples

package examples

import (
"errors"
"testing"

pipeline "github.com/ccremer/go-command-pipeline"
"github.com/stretchr/testify/assert"
)

func TestExample_Abort(t *testing.T) {
p := pipeline.NewPipeline()
p.WithSteps(
pipeline.NewStepFromFunc("abort demo", abort),
pipeline.NewStepFromFunc("never executed", doNotExecute),
)
result := p.Run()
assert.True(t, result.IsSuccessful())
}

func doNotExecute(_ pipeline.Context) error {
return errors.New("should not execute")
}

func abort(_ pipeline.Context) error {
// some logic that can handle errors, but you don't want to bubble up the error.

// terminate pipeline gracefully
return pipeline.ErrAbort
}
20 changes: 13 additions & 7 deletions pipeline.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pipeline

import (
"errors"
"fmt"
)

Expand Down Expand Up @@ -119,7 +120,8 @@ func (p *Pipeline) WithFinalizer(handler ResultHandler) *Pipeline {

// Run executes the pipeline and returns the result.
// Steps are executed sequentially as they were added to the Pipeline.
// If a Step returns a Result with a non-nil error, the Pipeline is aborted its Result contains the affected step's error.
// If a Step returns a Result with a non-nil error, the Pipeline is aborted and its Result contains the affected step's error.
// However, if Result.Err is wrapped in ErrAbort, then the pipeline is aborted, but the final Result.Err will be nil.
func (p *Pipeline) Run() Result {
result := p.doRun()
if p.finalizer != nil {
Expand All @@ -134,15 +136,19 @@ func (p *Pipeline) doRun() Result {
listener.Accept(step)
}

r := step.F(p.context)
result := step.F(p.context)
var err error
if step.H != nil {
if handlerErr := step.H(p.context, r); handlerErr != nil {
return Result{Err: fmt.Errorf("step '%s' failed: %w", step.Name, handlerErr)}
}
err = step.H(p.context, result)
} else {
if r.Err != nil {
return Result{Err: fmt.Errorf("step '%s' failed: %w", step.Name, r.Err)}
err = result.Err
}
if err != nil {
if errors.Is(err, ErrAbort) {
// Abort pipeline without error
return Result{}
}
return Result{Err: fmt.Errorf("step '%s' failed: %w", step.Name, err)}
}
}
return Result{}
Expand Down
13 changes: 13 additions & 0 deletions pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ func TestPipeline_Run(t *testing.T) {
expectedCalls: 1,
expectErrorString: "step failed",
},
"GivenStepWithErrAbort_WhenRunningWithErrAbort_ThenDoNotExecuteNextSteps": {
givenSteps: []Step{
NewStepFromFunc("test-step", func(_ Context) error {
callCount += 1
return ErrAbort
}),
NewStepFromFunc("step-should-not-execute", func(_ Context) error {
callCount += 1
return errors.New("should not execute")
}),
},
expectedCalls: 1,
},
"GivenSingleStepWithHandler_WhenRunningWithError_ThenAbortWithError": {
givenSteps: []Step{
NewStep("test-step", func(_ Context) Result {
Expand Down
5 changes: 5 additions & 0 deletions result.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package pipeline

import "errors"

// ErrAbort indicates that the pipeline should be terminated immediately without returning an error.
var ErrAbort = errors.New("abort")

// IsSuccessful returns true if the contained error is nil.
func (r Result) IsSuccessful() bool {
return r.Err == nil
Expand Down

0 comments on commit 492492b

Please sign in to comment.