Skip to content

Commit

Permalink
feat: add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mhmtszr committed Jul 5, 2024
1 parent f94ba9a commit 2a24a20
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
4 changes: 2 additions & 2 deletions pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ type Pipeline[K any] struct {
stepDelegate StepDelegate[K]
}

func (t Pipeline[K]) Execute(context K) {
t.stepDelegate(context)
func (t Pipeline[K]) Execute(context K) error {
return t.stepDelegate(context)
}
8 changes: 5 additions & 3 deletions pipeline_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ type Builder[K any] struct {
}

func (t Builder[K]) Build() Pipeline[K] {
var step StepDelegate[K] = func(context K) {}
var step StepDelegate[K] = func(context K) error {
return nil
}
for i := len(t.steps) - 1; i >= 0; i-- {
step = t.steps[i](step)
}
Expand All @@ -16,8 +18,8 @@ func (t Builder[K]) Build() Pipeline[K] {

func (t Builder[K]) UsePipelineStep(step Step[K]) Builder[K] {
t.steps = append(t.steps, func(next StepDelegate[K]) StepDelegate[K] {
return func(context K) {
step.Execute(context, next)
return func(context K) error {
return step.Execute(context, next)
}
})
return t
Expand Down
30 changes: 30 additions & 0 deletions pipeline_error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package pipeline_test

import (
"fmt"

Check failure on line 4 in pipeline_error_test.go

View workflow job for this annotation

GitHub Actions / build

File is not `gofumpt`-ed (gofumpt)

Check failure on line 4 in pipeline_error_test.go

View workflow job for this annotation

GitHub Actions / build

File is not `gofumpt`-ed (gofumpt)
"github.com/mhmtszr/pipeline"
"testing"

Check failure on line 6 in pipeline_error_test.go

View workflow job for this annotation

GitHub Actions / build

File is not `gofumpt`-ed (gofumpt)

Check failure on line 6 in pipeline_error_test.go

View workflow job for this annotation

GitHub Actions / build

File is not `gofumpt`-ed (gofumpt)
)

type (
SuccessStep struct{}
ErrorStep struct{}
)

func (s SuccessStep) Execute(context *int, next func(context *int) error) error {
return next(context)
}

func (e ErrorStep) Execute(context *int, next func(context *int) error) error {
return fmt.Errorf("errorstep error")
}

func TestErrorPipeline(t *testing.T) {
p := pipeline.Builder[*int]{}.UsePipelineStep(SuccessStep{}).UsePipelineStep(ErrorStep{}).Build()
nm := 3
err := p.Execute(&nm)
if err != nil && err.Error() == "errorstep error" {
return
}
t.Errorf("error step should return error")
}
4 changes: 2 additions & 2 deletions pipeline_step.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package pipeline

type StepDelegate[K any] func(context K)
type StepDelegate[K any] func(context K) error

type Step[K any] interface {
Execute(context K, next func(context K))
Execute(context K, next func(context K) error) error
}
13 changes: 7 additions & 6 deletions pipeline_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pipeline
package pipeline_test

import (

Check failure on line 3 in pipeline_test.go

View workflow job for this annotation

GitHub Actions / build

File is not `gofumpt`-ed (gofumpt)

Check failure on line 3 in pipeline_test.go

View workflow job for this annotation

GitHub Actions / build

File is not `gofumpt`-ed (gofumpt)
"github.com/mhmtszr/pipeline"
"testing"
)

Expand All @@ -9,18 +10,18 @@ type (
Add struct{}
)

func (s Square) Execute(context *int, next func(context *int)) {
func (s Square) Execute(context *int, next func(context *int) error) error {
*context = (*context) * (*context)
next(context)
return next(context)
}

func (a Add) Execute(context *int, next func(context *int)) {
func (a Add) Execute(context *int, next func(context *int) error) error {
*context = (*context) + (*context)
next(context)
return next(context)
}

func TestPipeline(t *testing.T) {
p := Builder[*int]{}.UsePipelineStep(Square{}).UsePipelineStep(Add{}).Build()
p := pipeline.Builder[*int]{}.UsePipelineStep(Square{}).UsePipelineStep(Add{}).Build()
nm := 3
want := 18
p.Execute(&nm)

Check failure on line 27 in pipeline_test.go

View workflow job for this annotation

GitHub Actions / build

Error return value of `p.Execute` is not checked (errcheck)

Check failure on line 27 in pipeline_test.go

View workflow job for this annotation

GitHub Actions / build

Error return value of `p.Execute` is not checked (errcheck)
Expand Down

0 comments on commit 2a24a20

Please sign in to comment.