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

optimize error in the project #379

Closed
luky116 opened this issue Dec 3, 2022 · 10 comments · Fixed by #395
Closed

optimize error in the project #379

luky116 opened this issue Dec 3, 2022 · 10 comments · Fixed by #395
Assignees

Comments

@luky116
Copy link
Contributor

luky116 commented Dec 3, 2022

What would you like to be added:
If you want to define an error variable, use errors.New, if it is a temporary error of return, use fmt.Errorf

如果定一个error变量,使用 errors.New 方法,如果是return一个临时error,使用fmt.Errorf

Why is this needed:

@mooleetzi
Copy link
Contributor

Hi ~ I'd like to work on this.

@luky116
Copy link
Contributor Author

luky116 commented Dec 3, 2022

Hi ~ I'd like to work on this.

Looking forward to your code

@mooleetzi
Copy link
Contributor

There are TccFenceError, two TransactionError in project.
If change them all to errors.New or fmt.Errorf, some check like err.(TccFenError) will not work.
What about generate a uniform error pkg to handle all err in this project?

@luky116
Copy link
Contributor Author

luky116 commented Dec 6, 2022

TccFenceError 这种常量error,用 errors.New 来定义,比较的时候用 errors.Is 方法来比较,这样能解答你的疑惑吗

@luky116
Copy link
Contributor Author

luky116 commented Dec 6, 2022

There are TccFenceError, two TransactionError in project. If change them all to errors.New or fmt.Errorf, some check like err.(TccFenError) will not work. What about generate a uniform error pkg to handle all err in this project?

可以参考下这个规范:https://github.com/luky116/uber_go_guide_cn#%E9%94%99%E8%AF%AF%E7%B1%BB%E5%9E%8B

@mooleetzi
Copy link
Contributor

OK, I see. Thx~

@luky116
Copy link
Contributor Author

luky116 commented Dec 6, 2022

OK, I see. Thx~

嗯嗯,建议是把error定义在需要用的文件,这样方便看代码

@Issues-translate-bot
Copy link

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


OK, I see. Thx~

Well, the suggestion is to define the error in the file that needs to be used, so that it is convenient to read the code

@mooleetzi
Copy link
Contributor

@luky116 cailao, 像这种

 errors.NewTccFenceError(errors.TccFenceDbDuplicateKeyError,
				fmt.Sprintf("Insert tcc fence record duplicate key exception. xid= %s, branchId= %d", tccFenceDo.Xid, tccFenceDo.BranchId),
				err)

需要上下文的error,就没办法仅使用errors.New和fmt.Errorf了呀,还是得自定义一下,这种error期待改成什么样呢

@mooleetzi mooleetzi mentioned this issue Dec 10, 2022
@106umao
Copy link
Contributor

106umao commented Jan 1, 2023

@luky116 cailao, 像这种

 errors.NewTccFenceError(errors.TccFenceDbDuplicateKeyError,
				fmt.Sprintf("Insert tcc fence record duplicate key exception. xid= %s, branchId= %d", tccFenceDo.Xid, tccFenceDo.BranchId),
				err)

需要上下文的error,就没办法仅使用errors.New和fmt.Errorf了呀,还是得自定义一下,这种error期待改成什么样呢

用这个库重构一下吧 pkg/errors

errors.Wrap(err,"context info")

这个 repo 好像没人维护了,可能需要讨论一下是否继续依赖,目前我们项目是依赖了的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants