mirror of
https://gitee.com/johng/gf.git
synced 2024-12-12 12:45:08 +08:00
feat(errors/gerror): add As
support (#4002)
This commit is contained in:
parent
5e47590165
commit
80e73da416
@ -98,6 +98,23 @@ func Is(err, target error) bool {
|
||||
return errors.Is(err, target)
|
||||
}
|
||||
|
||||
// As finds the first error in err's chain that matches target, and if so, sets
|
||||
// target to that error value and returns true.
|
||||
//
|
||||
// The chain consists of err itself followed by the sequence of errors obtained by
|
||||
// repeatedly calling Unwrap.
|
||||
//
|
||||
// An error matches target if the error's concrete value is assignable to the value
|
||||
// pointed to by target, or if the error has a method As(interface{}) bool such that
|
||||
// As(target) returns true. In the latter case, the As method is responsible for
|
||||
// setting target.
|
||||
//
|
||||
// As will panic if target is not a non-nil pointer to either a type that implements
|
||||
// error, or to any interface type. As returns false if err is nil.
|
||||
func As(err error, target any) bool {
|
||||
return errors.As(err, target)
|
||||
}
|
||||
|
||||
// HasError performs as Is.
|
||||
// This function is designed and implemented early before errors.Is of go stdlib.
|
||||
// Deprecated: use Is instead.
|
||||
|
@ -17,6 +17,22 @@ import (
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
)
|
||||
|
||||
// customError is used to test As function
|
||||
type customError struct {
|
||||
Message string
|
||||
}
|
||||
|
||||
func (e *customError) Error() string {
|
||||
return e.Message
|
||||
}
|
||||
|
||||
// anotherError is used to test As function with different error type
|
||||
type anotherError struct{}
|
||||
|
||||
func (e *anotherError) Error() string {
|
||||
return "another error"
|
||||
}
|
||||
|
||||
func nilError() error {
|
||||
return nil
|
||||
}
|
||||
@ -472,3 +488,56 @@ func Test_NewOption(t *testing.T) {
|
||||
}), gerror.New("NewOptionError"))
|
||||
})
|
||||
}
|
||||
|
||||
func Test_As(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var myerr = &customError{Message: "custom error"}
|
||||
|
||||
// Test with nil error
|
||||
var targetErr *customError
|
||||
t.Assert(gerror.As(nil, &targetErr), false)
|
||||
t.Assert(targetErr, nil)
|
||||
|
||||
// Test with standard error
|
||||
err1 := errors.New("standard error")
|
||||
t.Assert(gerror.As(err1, &targetErr), false)
|
||||
t.Assert(targetErr, nil)
|
||||
|
||||
// Test with custom error type
|
||||
err2 := myerr
|
||||
t.Assert(gerror.As(err2, &targetErr), true)
|
||||
t.Assert(targetErr.Message, "custom error")
|
||||
|
||||
// Test with wrapped error
|
||||
err3 := gerror.Wrap(myerr, "wrapped")
|
||||
targetErr = nil
|
||||
t.Assert(gerror.As(err3, &targetErr), true)
|
||||
t.Assert(targetErr.Message, "custom error")
|
||||
|
||||
// Test with deeply wrapped error
|
||||
err4 := gerror.Wrap(gerror.Wrap(gerror.Wrap(myerr, "wrap3"), "wrap2"), "wrap1")
|
||||
targetErr = nil
|
||||
t.Assert(gerror.As(err4, &targetErr), true)
|
||||
t.Assert(targetErr.Message, "custom error")
|
||||
|
||||
// Test with different error type
|
||||
var otherErr *anotherError
|
||||
t.Assert(gerror.As(err4, &otherErr), false)
|
||||
t.Assert(otherErr, nil)
|
||||
|
||||
// Test with non-pointer target
|
||||
defer func() {
|
||||
t.Assert(recover() != nil, true)
|
||||
}()
|
||||
var nonPtr customError
|
||||
gerror.As(err4, nonPtr)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// Test with nil target
|
||||
defer func() {
|
||||
t.Assert(recover() != nil, true)
|
||||
}()
|
||||
gerror.As(errors.New("error"), nil)
|
||||
})
|
||||
}
|
||||
|
@ -133,7 +133,7 @@ func doTestCronAddFixedPattern(t *testing.T) {
|
||||
array.Append(1)
|
||||
})
|
||||
t.AssertNil(err)
|
||||
time.Sleep(3000 * time.Millisecond)
|
||||
time.Sleep(3500 * time.Millisecond)
|
||||
g.Log().Debug(ctx, `current time`)
|
||||
t.Assert(array.Len(), 1)
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user