Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(errors/gerror): #3633 Is performs the same as errors.Is from go stdlib #3640

Merged
merged 3 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions errors/gerror/gerror.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ import (
"github.com/gogf/gf/v2/errors/gcode"
)

// IIs is the interface for Is feature.
type IIs interface {
Error() string
Is(target error) bool
}

// IEqual is the interface for Equal feature.
type IEqual interface {
Error() string
Expand Down
15 changes: 8 additions & 7 deletions errors/gerror/gerror_api_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package gerror

import (
"errors"
"runtime"
)

Expand Down Expand Up @@ -91,17 +92,17 @@ func Equal(err, target error) bool {
}

// Is reports whether current error `err` has error `target` in its chaining errors.
// It is just for implements for stdlib errors.Is from Go version 1.17.
// There's similar function HasError which is designed and implemented early before errors.Is of go stdlib.
// It is now alias of errors.Is of go stdlib, to guarantee the same performance as go stdlib.
func Is(err, target error) bool {
if e, ok := err.(IIs); ok {
return e.Is(target)
}
return false
return errors.Is(err, target)
}

// HasError is alias of Is, which more easily understanding semantics.
// HasError performs as Is.
// This function is designed and implemented early before errors.Is of go stdlib.
// Deprecated: use Is instead.
func HasError(err, target error) bool {
return Is(err, target)
return errors.Is(err, target)
}

// callers returns the stack callers.
Expand Down
19 changes: 0 additions & 19 deletions errors/gerror/gerror_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,3 @@ func (err *Error) Equal(target error) bool {
}
return true
}

// Is reports whether current error `err` has error `target` in its chaining errors.
// It is just for implements for stdlib errors.Is from Go version 1.17.
func (err *Error) Is(target error) bool {
if Equal(err, target) {
return true
}
nextErr := err.Unwrap()
if nextErr == nil {
return false
}
if Equal(nextErr, target) {
return true
}
if e, ok := nextErr.(IIs); ok {
return e.Is(target)
}
return false
}
2 changes: 1 addition & 1 deletion errors/gerror/gerror_z_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func ExampleIs() {
fmt.Println(gerror.Is(err1, err2))

// Output:
// false
// true
// true
// true
// false
Expand Down
22 changes: 20 additions & 2 deletions errors/gerror/gerror_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,10 +414,28 @@ func Test_Is(t *testing.T) {
err2 := gerror.Wrap(err1, "2")
err2 = gerror.Wrap(err2, "3")
t.Assert(gerror.Is(err2, err1), true)

var (
errNotFound = errors.New("not found")
gerror1 = gerror.Wrap(errNotFound, "wrapped")
gerror2 = gerror.New("not found")
)
t.Assert(errors.Is(errNotFound, errNotFound), true)
t.Assert(errors.Is(nil, errNotFound), false)
t.Assert(errors.Is(nil, nil), true)

t.Assert(gerror.Is(errNotFound, errNotFound), true)
t.Assert(gerror.Is(nil, errNotFound), false)
t.Assert(gerror.Is(nil, nil), true)

t.Assert(errors.Is(gerror1, errNotFound), true)
t.Assert(errors.Is(gerror2, errNotFound), false)
t.Assert(gerror.Is(gerror1, errNotFound), true)
t.Assert(gerror.Is(gerror2, errNotFound), false)
})
}

func Test_HashError(t *testing.T) {
func Test_HasError(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
err1 := errors.New("1")
err2 := gerror.Wrap(err1, "2")
Expand All @@ -426,7 +444,7 @@ func Test_HashError(t *testing.T) {
})
}

func Test_HashCode(t *testing.T) {
func Test_HasCode(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(gerror.HasCode(nil, gcode.CodeNotAuthorized), false)
err1 := errors.New("1")
Expand Down