Skip to content

Commit

Permalink
Merge pull request #81 from louvri/handling_loop_error
Browse files Browse the repository at this point in the history
Handling loop error with array
  • Loading branch information
johnjerrico authored Oct 22, 2024
2 parents 5036e96 + 4787dc7 commit 18c553b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
7 changes: 5 additions & 2 deletions loop_array/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func Middleware(e endpoint.Endpoint, preprocessor func(data interface{}) interface{}, postprocessor func(original, data interface{}, err error), opts ...RUN_WITH_OPTION.Option) endpoint.Middleware {
return func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, req interface{}) (interface{}, error) {
errorCollection := map[int]interface{}{}
errorCollection := make([]map[string]interface{}, 0)

opt := make(map[RUN_WITH_OPTION.Option]bool)
for _, option := range opts {
Expand Down Expand Up @@ -52,7 +52,10 @@ func Middleware(e endpoint.Endpoint, preprocessor func(data interface{}) interfa
if !opt[RUN_WITH_OPTION.RUN_WITH_ERROR] {
return nil, err
}
errorCollection[index] = err.Error()
errorCollection = append(errorCollection, map[string]interface{}{
"lineNumber": index,
"error": err.Error(),
})

}
if postprocessor != nil {
Expand Down
6 changes: 3 additions & 3 deletions loop_array/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestLoopArrayWithErrorAndIgnore(t *testing.T) {
),
)(m.Main)(context.Background(), "execute")
if r != nil {
decoded := make(map[int]interface{}, 0)
decoded := make([]map[string]interface{}, 0)
if curr := json.Unmarshal([]byte(r.Error()), &decoded); curr != nil {
t.Log("error should be able decoded to array interface")
t.FailNow()
Expand Down Expand Up @@ -222,7 +222,7 @@ func TestLoopRunWithError(t *testing.T) {
return data
}, func(original, data interface{}, err error) {
// no op
}),
}, option.RUN_WITH_ERROR),
)(func(context.Context, interface{}) (interface{}, error) {
return []interface{}{
"1stIndex",
Expand All @@ -239,7 +239,7 @@ func TestLoopRunWithError(t *testing.T) {
}

if err != nil {
decoded := make(map[int]interface{}, 0)
decoded := make([]map[string]interface{}, 0)
if curr := json.Unmarshal([]byte(err.Error()), &decoded); curr != nil {
t.Log("error should be able decoded to array interface")
t.FailNow()
Expand Down
7 changes: 5 additions & 2 deletions loop_next/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func Middleware(
var err error
var response interface{}
curr = make([]map[string]interface{}, 0)
errorCollection := map[int]interface{}{}
errorCollection := make([]map[string]interface{}, 0)

prevRequest := req

Expand All @@ -65,7 +65,10 @@ func Middleware(
if !opt[RUN_WITH_OPTION.RUN_WITH_ERROR] {
return nil, err
}
errorCollection[index] = err.Error()
errorCollection = append(errorCollection, map[string]interface{}{
"lineNumber": index,
"error": err.Error(),
})
}
}

Expand Down
2 changes: 1 addition & 1 deletion loop_next/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestLoopNext(t *testing.T) {
}

if err != nil {
decoded := make(map[int]interface{}, 0)
decoded := make([]map[string]interface{}, 0)
if curr := json.Unmarshal([]byte(err.Error()), &decoded); curr != nil {
t.Log("error should be able decoded to array interface")
t.FailNow()
Expand Down

0 comments on commit 18c553b

Please sign in to comment.