Commit 5a1a06b7 authored by suyanlong's avatar suyanlong

😋 error select and combine

parent d1c21a7b
Pipeline #8152 canceled with stages
...@@ -15,3 +15,4 @@ goent.sum ...@@ -15,3 +15,4 @@ goent.sum
testdata/ testdata/
!internal/checker/testdata !internal/checker/testdata
pkg/filter/build/
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"runtime" "runtime"
"github.com/hashicorp/go-multierror"
"github.com/juju/errors" "github.com/juju/errors"
) )
...@@ -19,3 +20,16 @@ func TraceError(err error, skip int) error { ...@@ -19,3 +20,16 @@ func TraceError(err error, skip int) error {
} }
return fmt.Errorf("%s:%d: %s", file, line, err) return fmt.Errorf("%s:%d: %s", file, line, err)
} }
func CombineError(errs ...error) error {
return multierror.Append(&multierror.Error{}, errs...).ErrorOrNil()
}
func SelectError(errs ...error) error {
for _, err := range errs {
if err != nil {
return err
}
}
return nil
}
package tool
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCombineError(t *testing.T) {
err := CombineError(nil, nil)
assert.Nil(t, err)
err = CombineError(fmt.Errorf("err info"), nil)
assert.NotNil(t, err)
err = CombineError(nil, fmt.Errorf("err info"), nil)
assert.NotNil(t, err)
t.Log(err)
}
func TestSelectError(t *testing.T) {
err1 := fmt.Errorf("err info1")
err2 := fmt.Errorf("err info2")
err := SelectError(nil, nil)
assert.Nil(t, err)
err = SelectError(err1, nil, err2)
assert.Equal(t, err, err1)
err = SelectError(nil, err2, nil, err1)
assert.Equal(t, err, err2)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment