Skip to content

Commit

Permalink
👔 update: errorx - add new func: MustEX, and add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 19, 2023
1 parent dd238bb commit d9ad4c6
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 6 deletions.
6 changes: 3 additions & 3 deletions errorx/assert.go
Expand Up @@ -53,9 +53,9 @@ func NotIn[T comdef.ScalarType](value T, list []T, fmtAndArgs ...any) error {
return nil
}

func formatErrMsg(errMsg string, fmtAndArgs []any) string {
func formatErrMsg(defMsg string, fmtAndArgs []any) string {
if len(fmtAndArgs) > 0 {
errMsg = comfunc.FormatWithArgs(fmtAndArgs)
return comfunc.FormatWithArgs(fmtAndArgs)
}
return errMsg
return defMsg
}
2 changes: 2 additions & 0 deletions errorx/assert_test.go
Expand Up @@ -19,6 +19,8 @@ func TestAssert_methods(t *testing.T) {
// IsIn
assert.NoErr(t, errorx.IsIn(1, []int{1, 2, 3}))
assert.Err(t, errorx.IsIn(4, []int{1, 2, 3}))
assert.Err(t, errorx.IsIn(4, []int{1, 2, 3}, "error msg"))
assert.Err(t, errorx.IsIn(4, []int{1, 2, 3}, "error %s", "msg"))

// NotIn
assert.NoErr(t, errorx.NotIn(4, []int{1, 2, 3}))
Expand Down
1 change: 1 addition & 0 deletions errorx/errors_test.go
Expand Up @@ -48,6 +48,7 @@ func TestErrMap_usage(t *testing.T) {
assert.False(t, em.IsEmpty())
assert.NotEmpty(t, em.Error())
assert.Err(t, em.ErrorOrNil())
assert.Err(t, em.One())
}

func TestErrors_usage(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions errorx/errorx.go
Expand Up @@ -265,6 +265,7 @@ func WithStack(err error) error {
if err == nil {
return nil
}

return &ErrorX{
msg: err.Error(),
// prev: err,
Expand All @@ -277,6 +278,7 @@ func Traced(err error) error {
if err == nil {
return nil
}

return &ErrorX{
msg: err.Error(),
stack: callersStack(stdOpt.SkipDepth, stdOpt.TraceDepth),
Expand All @@ -288,6 +290,7 @@ func Stacked(err error) error {
if err == nil {
return nil
}

return &ErrorX{
msg: err.Error(),
stack: callersStack(stdOpt.SkipDepth, stdOpt.TraceDepth),
Expand Down
25 changes: 25 additions & 0 deletions errorx/errorx_test.go
Expand Up @@ -137,6 +137,7 @@ func TestWithPrev_errorx_l2(t *testing.T) {

func TestStacked_goerr(t *testing.T) {
assert.Nil(t, errorx.Stacked(nil))
assert.Nil(t, errorx.Traced(nil))

err1 := errorx.E("first error message")
assert.Err(t, err1)
Expand Down Expand Up @@ -165,6 +166,8 @@ func TestStacked_errorx(t *testing.T) {
err2 := errorx.WithStack(err1)
assert.Err(t, err2)
fmt.Printf("%+v\n", err2)

assert.Nil(t, errorx.WithStack(nil))
}

func TestTo_ErrorX(t *testing.T) {
Expand Down Expand Up @@ -235,12 +238,34 @@ func TestCause(t *testing.T) {

func TestWrapf(t *testing.T) {
err := errorx.Rawf("first error %s", "message")
assert.Panics(t, func() {
_ = errorx.MustEX(err)
})

err = errorx.Wrapf(err, "second error %s", "message")
assert.Err(t, err)
assert.True(t, errorx.IsErrorX(err))

ex, ok := errorx.ToErrorX(err)
assert.True(t, ok)
assert.Eq(t, "second error message", ex.Message())

fmt.Println(err)
fmt.Println("err.Error():")
fmt.Println(err.Error())

err = errorx.Wrapf(nil, "first error %s", "message")
assert.Eq(t, "first error message", err.Error())
}

func TestWithOptions(t *testing.T) {
err := errorx.WithOptions("err msg", errorx.SkipDepth(3), errorx.TraceDepth(10))
// fmt.Println(err.Error())
assert.Eq(t, "err msg", err.Error())
assert.True(t, errorx.IsErrorX(err))

s := errorx.MustEX(err).StackString()
assert.StrContains(t, s, "TestWithOptions")
}

func TestErrorX_Format(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions errorx/stack.go
Expand Up @@ -99,7 +99,7 @@ func FuncForPC(pc uintptr) *Func {
}
}

// FileLine of the func
// FileLine returns the file name and line number of the source code
func (f *Func) FileLine() (file string, line int) {
return f.Func.FileLine(f.pc)
}
Expand All @@ -108,7 +108,7 @@ func (f *Func) FileLine() (file string, line int) {
//
// Returns eg:
//
// github.com/gookit/goutil/errorx_test.TestWithPrev(), errorx_test.go:34
// "github.com/gookit/goutil/errorx_test.TestWithPrev(), errorx_test.go:34"
func (f *Func) Location() string {
file, line := f.FileLine()

Expand Down
1 change: 0 additions & 1 deletion errorx/stack_test.go
Expand Up @@ -17,7 +17,6 @@ func TestErrStackOpt(t *testing.T) {
Config(SkipDepth(5), TraceDepth(12))
assert.Eq(t, 5, stdOpt.SkipDepth)
assert.Eq(t, 12, stdOpt.TraceDepth)

}

func TestFuncForPC(t *testing.T) {
Expand Down
9 changes: 9 additions & 0 deletions errorx/util.go
Expand Up @@ -80,6 +80,15 @@ func ToErrorX(err error) (ex *ErrorX, ok bool) {
return
}

// MustEX convert error to *ErrorX, panic if err check failed.
func MustEX(err error) *ErrorX {
ex, ok := err.(*ErrorX)
if !ok {
panic("errorx: error is not *ErrorX")
}
return ex
}

// Has contains target error, or err is eq target.
// alias of errors.Is()
func Has(err, target error) bool {
Expand Down

0 comments on commit d9ad4c6

Please sign in to comment.