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

gerror: compatibility standard library error , IIs interface not implemented #3635

Closed
wants to merge 2 commits into from
Closed
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
10 changes: 7 additions & 3 deletions errors/gerror/gerror_api_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,14 @@ 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.
func Is(err, target error) bool {
if e, ok := err.(IIs); ok {
return e.Is(target)
if err == nil {
return err == target
}
return false
e, ok := err.(IIs)
if !ok {
e = Wrap(err, "").(IIs)
}
return e.Is(target)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是否这样改动更合适一些:

e, ok := err.(IIs)
if ok {
    return e.Is(target)
}
return err == target

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果改成这样,这个测试用例就过不了:t.Assert(gerror.Is(errors.New("1"), gerror.New("1")), true)

Copy link
Member

@gqcn gqcn Jun 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果改成这样,这个测试用例就过不了:t.Assert(gerror.Is(errors.New("1"), gerror.New("1")), true)

看了下,这个t.Assert(gerror.Is(errors.New("1"), gerror.New("1")), true)本来就不应该过。我新建了分支合并了你的提交新建了pr到这里 #3640 ,这个pr就可以关闭了。

}

// HasError is alias of Is, which more easily understanding semantics.
Expand Down
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
9 changes: 7 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,15 @@ func Test_Is(t *testing.T) {
err2 := gerror.Wrap(err1, "2")
err2 = gerror.Wrap(err2, "3")
t.Assert(gerror.Is(err2, err1), true)

ErrNotFound := errors.New("not found")
t.Assert(gerror.Is(ErrNotFound, ErrNotFound), true)
t.Assert(gerror.Is(nil, ErrNotFound), false)
t.Assert(gerror.Is(nil, nil), true)
})
}

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 +431,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
Loading