Skip to content

Commit

Permalink
refactor: remove MergeErrorsWithTag method
Browse files Browse the repository at this point in the history
Signed-off-by: Jiawei Zhao <Phoenxi500526@163.com>
  • Loading branch information
Jiawei Zhao authored and Jiawei Zhao committed Jan 14, 2024
1 parent 8e68fc1 commit c74fbf6
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 39 deletions.
5 changes: 2 additions & 3 deletions internal/controller/sharded.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,13 @@ func (r *StCtrlErrSet) AsResult() (ctrl.Result, error) {
if r.Update != nil {
errMap["update-status"] = r.Update
}
mergedErr := util.MergeErrorsWithTag(errMap)
if mergedErr == nil {
if len(errMap) == 0 {
if updateConflict {
return ctrl.Result{Requeue: true}, nil
} else {
return ctrl.Result{}, nil
}
} else {
return ctrl.Result{Requeue: true}, mergedErr
return ctrl.Result{Requeue: true}, &util.MultiTaggedError{Errors: errMap}
}
}
14 changes: 0 additions & 14 deletions internal/util/mutierror.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,3 @@ func (e *MultiTaggedError) Error() string {
}
return strings.Join(errStrs, "; ")
}

// MergeErrorsWithTag merges multiple errors into one with tags.
func MergeErrorsWithTag(errors map[string]error) *MultiTaggedError {
errMap := make(map[string]error)
for tag, err := range errors {
if err != nil {
errMap[tag] = err
}
}
if len(errMap) == 0 {
return nil
}
return &MultiTaggedError{Errors: errMap}
}
24 changes: 2 additions & 22 deletions internal/util/mutierror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ package util

import (
"fmt"
"github.com/stretchr/testify/assert"
"k8s.io/utils/strings/slices"
"testing"

"github.com/stretchr/testify/assert"
)

func TestMergeErrors(t *testing.T) {
Expand All @@ -38,23 +38,3 @@ func TestMergeErrors(t *testing.T) {
err4 := MergeErrors(nil, nil)
assert.Nil(t, err4)
}

func TestMergeErrorsWithTags(t *testing.T) {
err1 := MergeErrorsWithTag(map[string]error{
"tag1": fmt.Errorf("foo"),
"tag2": fmt.Errorf("bar"),
})
assert.True(t, slices.Contains([]string{"[tag1] foo; [tag2] bar", "[tag2] bar; [tag1] foo"}, err1.Error()))

err2 := MergeErrorsWithTag(map[string]error{
"tag1": fmt.Errorf("foo"),
"tag2": nil,
})
assert.Equal(t, "[tag1] foo", err2.Error())

err3 := MergeErrorsWithTag(map[string]error{
"tag1": nil,
"tag2": nil,
})
assert.Nil(t, err3)
}

0 comments on commit c74fbf6

Please sign in to comment.