diff options
| author | stacktasec <stacktasec@gmail.com> | 2020-03-09 16:02:48 +0800 |
|---|---|---|
| committer | stacktasec <stacktasec@gmail.com> | 2020-03-09 16:02:48 +0800 |
| commit | e704bc527b1e1bf25e6cf307a38bab1807ff5143 (patch) | |
| tree | b5c4933f7c81f3197974eaf2b365178b892102bb | |
| parent | Merge remote-tracking branch 'upstream/master' into add_printbyline_func (diff) | |
| download | kubeedge-e704bc527b1e1bf25e6cf307a38bab1807ff5143.tar.gz | |
only receive type '[]error'
| -rw-r--r-- | cloud/cmd/cloudcore/app/server.go | 2 | ||||
| -rw-r--r-- | edge/cmd/edgecore/app/server.go | 2 | ||||
| -rw-r--r-- | edgesite/cmd/edgesite/app/server.go | 2 | ||||
| -rw-r--r-- | pkg/util/uitl_test.go | 59 | ||||
| -rw-r--r-- | pkg/util/util.go | 30 |
5 files changed, 10 insertions, 85 deletions
diff --git a/cloud/cmd/cloudcore/app/server.go b/cloud/cmd/cloudcore/app/server.go index 2d82ad436..9a1af81b0 100644 --- a/cloud/cmd/cloudcore/app/server.go +++ b/cloud/cmd/cloudcore/app/server.go @@ -47,7 +47,7 @@ kubernetes controller which manages devices so that the device metadata/status d klog.Fatal(err) } - if errs := validation.ValidateCloudCoreConfiguration(config); len(errs) > 0 { + if errs := validation.ValidateCloudCoreConfiguration(config).ToAggregate().Errors(); len(errs) > 0 { klog.Fatal(util.SpliceErrors(errs)) } diff --git a/edge/cmd/edgecore/app/server.go b/edge/cmd/edgecore/app/server.go index 017a61ecf..e4d4a60da 100644 --- a/edge/cmd/edgecore/app/server.go +++ b/edge/cmd/edgecore/app/server.go @@ -62,7 +62,7 @@ offering HTTP client capabilities to components of cloud to reach HTTP servers r klog.Fatal(err) } - if errs := validation.ValidateEdgeCoreConfiguration(config); len(errs) > 0 { + if errs := validation.ValidateEdgeCoreConfiguration(config).ToAggregate().Errors(); len(errs) > 0 { klog.Fatal(util.SpliceErrors(errs)) } diff --git a/edgesite/cmd/edgesite/app/server.go b/edgesite/cmd/edgesite/app/server.go index 6be60eec0..63bd651d8 100644 --- a/edgesite/cmd/edgesite/app/server.go +++ b/edgesite/cmd/edgesite/app/server.go @@ -47,7 +47,7 @@ runs on edge nodes and manages containerized applications.`, klog.Fatal(err) } - if errs := validation.ValidateEdgeSiteConfiguration(config); len(errs) > 0 { + if errs := validation.ValidateEdgeSiteConfiguration(config).ToAggregate().Errors(); len(errs) > 0 { klog.Fatal(util.SpliceErrors(errs)) } diff --git a/pkg/util/uitl_test.go b/pkg/util/uitl_test.go index df50e3c28..3af2e775a 100644 --- a/pkg/util/uitl_test.go +++ b/pkg/util/uitl_test.go @@ -3,7 +3,6 @@ package util import ( "errors" "fmt" - "k8s.io/apimachinery/pkg/util/validation/field" "strings" "testing" ) @@ -19,38 +18,7 @@ func TestSpliceErrors(t *testing.T) { var line3 = fmt.Sprintf(" %s\n", err3) const tail = "]\n" - errList := field.ErrorList{} - errList = append(errList, - field.InternalError(field.NewPath("test path 1"), err1), - field.InternalError(field.NewPath("test path 2"), err2), - field.InternalError(field.NewPath("test path 3"), err3), - ) - - // case 1: none error - if SpliceErrors("") != "" || - SpliceErrors("test text") != "" || - SpliceErrors([]int{1}) != "" || - SpliceErrors([]int{1, 2, 3}) != "" { - t.Error("the func format the none error unexpected") - return - } - - // case 2: single error - singleOutput := SpliceErrors(err1) - if singleOutput != err1.Error() { - t.Error("the func format the single error unexpected") - return - } - - // case 3-1: single element error slice - sliceOutput := SpliceErrors([]error{err1}) - if sliceOutput != err1.Error() { - t.Error("the func format the single element error slice unexpected") - return - } - - // case 3-2: multiple elements error slice - sliceOutput = SpliceErrors([]error{err1, err2, err3}) + sliceOutput := SpliceErrors([]error{err1, err2, err3}) if strings.Index(sliceOutput, head) != 0 || strings.Index(sliceOutput, line1) != len(head) || strings.Index(sliceOutput, line2) != len(head+line1) || @@ -60,29 +28,8 @@ func TestSpliceErrors(t *testing.T) { return } - // case 4: single complex error - if SpliceErrors(errList[0]) != errList[0].Error() { - t.Error("the func format the single complex error unexpected") - return - } - - // case 5-1: single element complex error slice - if SpliceErrors(field.ErrorList{errList[0]}) != errList[0].Error() { - t.Error("the func format the single element complex error slice unexpected") - return - } - - // case 5-2: multiple element complex error slice - cpx1 := fmt.Sprintf(" %s\n", errList[0].Error()) - cpx2 := fmt.Sprintf(" %s\n", errList[1].Error()) - cpx3 := fmt.Sprintf(" %s\n", errList[2].Error()) - complexOutput := SpliceErrors(errList) - if strings.Index(complexOutput, head) != 0 || - strings.Index(complexOutput, cpx1) != len(head) || - strings.Index(complexOutput, cpx2) != len(head+cpx1) || - strings.Index(complexOutput, cpx3) != len(head+cpx1+cpx2) || - strings.Index(complexOutput, tail) != len(head+cpx1+cpx2+cpx3) { - t.Error("the func format the multiple element complex error slice unexpected") + if SpliceErrors([]error{}) != "" || SpliceErrors(nil) != "" { + t.Error("the func format the zero-length error slice unexpected") return } } diff --git a/pkg/util/util.go b/pkg/util/util.go index 0d4199bf8..7e301c692 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -25,7 +25,6 @@ import ( "os" "os/exec" "path/filepath" - "reflect" "strings" "time" @@ -516,35 +515,14 @@ func ReadDirNoStat(dirname string) ([]string, error) { return f.Readdirnames(-1) } -func SpliceErrors(errors interface{}) string { - t := reflect.TypeOf(errors) - var errStrings []string - switch t.Kind() { - case reflect.Slice, reflect.Array: - v := reflect.ValueOf(errors) - for i := 0; i < v.Len(); i++ { - if err, ok := v.Index(i).Interface().(error); ok { - errStrings = append(errStrings, err.Error()) - } - } - default: - if err, ok := reflect.ValueOf(errors).Interface().(error); ok { - errStrings = append(errStrings, err.Error()) - } - } - - if len(errStrings) == 0 { +func SpliceErrors(errors []error) string { + if len(errors) == 0 { return "" } - - if len(errStrings) == 1 { - return errStrings[0] - } - var stb strings.Builder stb.WriteString("[\n") - for _, errString := range errStrings { - stb.WriteString(fmt.Sprintf(" %s\n", errString)) + for _, err := range errors { + stb.WriteString(fmt.Sprintf(" %s\n", err.Error())) } stb.WriteString("]\n") return stb.String() |
