improve package gerror

This commit is contained in:
jflyfox
2020-12-30 13:18:43 +08:00
parent bdf23ef48f
commit 86e70ad55c
6 changed files with 189 additions and 15 deletions

View File

@ -1,4 +1,4 @@
// Copyright GoFrame gf Author(https://github.com/gogf/gf). All Rights Reserved.
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
@ -92,7 +92,7 @@ func Wrap(err error, text string) error {
error: err,
stack: callers(),
text: text,
code: -1,
code: Code(err),
}
}
@ -107,7 +107,37 @@ func Wrapf(err error, format string, args ...interface{}) error {
error: err,
stack: callers(),
text: fmt.Sprintf(format, args...),
code: -1,
code: Code(err),
}
}
// WrapSkip wraps error with text.
// It returns nil if given err is nil.
// The parameter <skip> specifies the stack callers skipped amount.
func WrapSkip(skip int, err error, text string) error {
if err == nil {
return nil
}
return &Error{
error: err,
stack: callers(skip),
text: text,
code: Code(err),
}
}
// WrapSkipf wraps error with text that is formatted with given format and args.
// It returns nil if given err is nil.
// The parameter <skip> specifies the stack callers skipped amount.
func WrapSkipf(skip int, err error, format string, args ...interface{}) error {
if err == nil {
return nil
}
return &Error{
error: err,
stack: callers(skip),
text: fmt.Sprintf(format, args...),
code: Code(err),
}
}
@ -177,6 +207,36 @@ func WrapCodef(code int, err error, format string, args ...interface{}) error {
}
}
// WrapCodeSkip wraps error with code and text.
// It returns nil if given err is nil.
// The parameter <skip> specifies the stack callers skipped amount.
func WrapCodeSkip(code, skip int, err error, text string) error {
if err == nil {
return nil
}
return &Error{
error: err,
stack: callers(skip),
text: text,
code: code,
}
}
// WrapCodeSkipf wraps error with code and text that is formatted with given format and args.
// It returns nil if given err is nil.
// The parameter <skip> specifies the stack callers skipped amount.
func WrapCodeSkipf(code, skip int, err error, format string, args ...interface{}) error {
if err == nil {
return nil
}
return &Error{
error: err,
stack: callers(skip),
text: fmt.Sprintf(format, args...),
code: code,
}
}
// Cause returns the error code of current error.
// It returns -1 if it has no error code or it does not implements interface Code.
func Code(err error) int {

View File

@ -1,4 +1,4 @@
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
@ -26,6 +26,29 @@ func Test_Nil(t *testing.T) {
})
}
func Test_New(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
err := gerror.New("1")
t.AssertNE(err, nil)
t.Assert(err.Error(), "1")
})
gtest.C(t, func(t *gtest.T) {
err := gerror.Newf("%d", 1)
t.AssertNE(err, nil)
t.Assert(err.Error(), "1")
})
gtest.C(t, func(t *gtest.T) {
err := gerror.NewSkip(1, "1")
t.AssertNE(err, nil)
t.Assert(err.Error(), "1")
})
gtest.C(t, func(t *gtest.T) {
err := gerror.NewSkipf(1, "%d", 1)
t.AssertNE(err, nil)
t.Assert(err.Error(), "1")
})
}
func Test_Wrap(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
err := errors.New("1")
@ -49,6 +72,75 @@ func Test_Wrap(t *testing.T) {
})
}
func Test_Wrapf(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
err := errors.New("1")
err = gerror.Wrapf(err, "%d", 2)
err = gerror.Wrapf(err, "%d", 3)
t.AssertNE(err, nil)
t.Assert(err.Error(), "3: 2: 1")
})
gtest.C(t, func(t *gtest.T) {
err := gerror.New("1")
err = gerror.Wrapf(err, "%d", 2)
err = gerror.Wrapf(err, "%d", 3)
t.AssertNE(err, nil)
t.Assert(err.Error(), "3: 2: 1")
})
gtest.C(t, func(t *gtest.T) {
err := gerror.New("1")
err = gerror.Wrapf(err, "")
t.AssertNE(err, nil)
t.Assert(err.Error(), "1")
})
}
func Test_WrapSkip(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
err := errors.New("1")
err = gerror.WrapSkip(1, err, "2")
err = gerror.WrapSkip(1, err, "3")
t.AssertNE(err, nil)
t.Assert(err.Error(), "3: 2: 1")
})
gtest.C(t, func(t *gtest.T) {
err := gerror.New("1")
err = gerror.WrapSkip(1, err, "2")
err = gerror.WrapSkip(1, err, "3")
t.AssertNE(err, nil)
t.Assert(err.Error(), "3: 2: 1")
})
gtest.C(t, func(t *gtest.T) {
err := gerror.New("1")
err = gerror.WrapSkip(1, err, "")
t.AssertNE(err, nil)
t.Assert(err.Error(), "1")
})
}
func Test_WrapSkipf(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
err := errors.New("1")
err = gerror.WrapSkipf(1, err, "2")
err = gerror.WrapSkipf(1, err, "3")
t.AssertNE(err, nil)
t.Assert(err.Error(), "3: 2: 1")
})
gtest.C(t, func(t *gtest.T) {
err := gerror.New("1")
err = gerror.WrapSkipf(1, err, "2")
err = gerror.WrapSkipf(1, err, "3")
t.AssertNE(err, nil)
t.Assert(err.Error(), "3: 2: 1")
})
gtest.C(t, func(t *gtest.T) {
err := gerror.New("1")
err = gerror.WrapSkipf(1, err, "")
t.AssertNE(err, nil)
t.Assert(err.Error(), "1")
})
}
func Test_Cause(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
err := errors.New("1")
@ -201,4 +293,18 @@ func Test_Code(t *testing.T) {
t.Assert(gerror.Code(err), 1)
t.Assert(err.Error(), "3: 2: 1")
})
gtest.C(t, func(t *gtest.T) {
err := errors.New("1")
err = gerror.Wrap(err, "2")
err = gerror.WrapCodeSkip(1, 100, err, "3")
t.Assert(gerror.Code(err), 1)
t.Assert(err.Error(), "3: 2: 1")
})
gtest.C(t, func(t *gtest.T) {
err := errors.New("1")
err = gerror.Wrap(err, "2")
err = gerror.WrapCodeSkipf(1, 100, err, "%s", "3")
t.Assert(gerror.Code(err), 1)
t.Assert(err.Error(), "3: 2: 1")
})
}