-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from ccremer/options-wrap
Add option to disable error wrapping
- Loading branch information
Showing
3 changed files
with
52 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package pipeline | ||
|
||
// Option configures the given Pipeline with a behaviour-altering setting. | ||
type Option func(pipeline *Pipeline) | ||
|
||
// WithOptions configures the Pipeline with settings. | ||
// The options are applied immediately. | ||
// Options are applied to nested pipelines provided they are set before building the nested pipeline. | ||
// Nested pipelines can be configured with their own options. | ||
func (p *Pipeline) WithOptions(options ...Option) *Pipeline { | ||
for _, option := range options { | ||
option(p) | ||
} | ||
return p | ||
} | ||
|
||
// DisableErrorWrapping disables the wrapping of errors that are emitted from pipeline steps. | ||
// This effectively causes Result.Err to be exactly the error as returned from a step. | ||
var DisableErrorWrapping Option = func(pipeline *Pipeline) { | ||
pipeline.disableErrorWrapping = true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package pipeline | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPipeline_WithOptions(t *testing.T) { | ||
t.Run("DisableErrorWrapping", func(t *testing.T) { | ||
p := NewPipeline().WithOptions(DisableErrorWrapping) | ||
p.WithSteps( | ||
NewStepFromFunc("disabled error wrapping", func(_ Context) error { | ||
return errors.New("some error") | ||
}), | ||
) | ||
assert.True(t, p.disableErrorWrapping) | ||
result := p.Run() | ||
assert.Equal(t, "some error", result.Err.Error()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters