gogf / gf

GoFrame is a modular, powerful, high-performance and enterprise-class application development framework of Golang.
https://goframe.org
MIT License
11.8k stars 1.61k forks source link

gerror.NewCode has different returns in different gf versions #3699

Closed xiaoxintiaoda closed 2 months ago

xiaoxintiaoda commented 4 months ago

Go version

go version go1.22.0 darwin/arm64

GoFrame version

2.7.2

Can this bug be reproduced with the latest release?

Option Yes

What did you do?

# 2.7.0
var err1 = gerror.NewCode(gcode.New(1000, "测试", nil), "测试")
var err2 = gerror.NewCode(gcode.New(1000, "测试", nil), "测试")
fmt.Println(errors.Is(err1, err2)) // return true
# 2.7.2
var err1 = gerror.NewCode(gcode.New(1000, "测试", nil), "测试")
var err2 = gerror.NewCode(gcode.New(1000, "测试", nil), "测试")
fmt.Println(errors.Is(err1, err2)) // return false

What did you see happen?

在2.7.0版本和2.7.2版本表现有差异

What did you expect to see?

在2.7.0版本和2.7.2版本应该都返回true

shuqingzai commented 4 months ago

See: #3640

xiaoxintiaoda commented 4 months ago

不好意思,那我想咨询一下,以下结果对你们来说是预期内的吗,因为我们之前使用2.7.0和2.7.1版本,有一些就是使用的这种错误判断,判断结果都是true。但是使用的后续2.7.2,判断结果都是false,那可能我需要做一些修改

# 2.7.2
var err1 = gerror.NewCode(gcode.New(1000, "测试", nil), "测试")
var err2 = gerror.NewCode(gcode.New(1000, "测试", nil), "测试")
fmt.Println(errors.Is(err1, err2)) // return false
Issues-translate-bot commented 4 months ago

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Sorry, I would like to ask. The following results are expected, because we used version 2.7.0 before, and some of them used this kind of wrong judgment, and the judgment results were all true. If the judgment results are false in subsequent 2.7.2 and later versions, then I may need to make some modifications.

# 2.7.2
var err1 = gerror.NewCode(gcode.New(1000, "test", nil), "test")
var err2 = gerror.NewCode(gcode.New(1000, "test", nil), "test")
fmt.Println(errors.Is(err1, err2)) // return false
xiaoxintiaoda commented 4 months ago

See: #3640

我看了这个pr,但是pr内容是修改的gerror.Is 方法,我是直接使用的errors.Is方法判断的

Issues-translate-bot commented 4 months ago

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


See: #3640

I read this pr, but the pr content is the modified gerror.Is method. I directly use the errors.Is method to judge.

shuqingzai commented 4 months ago

See: #3640

我看了这个pr,但是pr内容是修改的gerror.Is 方法,我是直接使用的errors.Is方法判断的

从逻辑上来说,是符合预期,因为你初始化 gerror 时,gerror.Error.error == nil ,在标准库中,使用 errors.Is 且实现 Unwrap ,返回 nil 就会判定为 false

See: https://github.com/gogf/gf/blob/ba322ba0acbb835a379d10581bf03b30b7c4fefc/errors/gerror/gerror_error.go#L107

如果两个 code 完全一致,你应该可以尝试使用 gerror.HasCode

Issues-translate-bot commented 4 months ago

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


See: #3640

I read this pr, but the pr content is the modified gerror.Is method. I directly use the errors.Is method to judge.

Logically speaking, it is in line with expectations, because when you initialize gerror, gerror.Error.error == nil, in the standard library, use errors.Is and implement Unwrap, returning nil will be judged as false

See: https://github.com/gogf/gf/blob/ba322ba0acbb835a379d10581bf03b30b7c4fefc/errors/gerror/gerror_error.go#L107

If the two codes are exactly the same, you should try using gerror.HasCode