英文:
How to know variable name adhering to go error
问题
在使用Go语言时,有一种模式用于定义错误并以一种(对我来说)非常特殊的方式处理它们。通常,错误会被声明为ErrorSomethingWentWrong = errors.New("Just an example!")
。你可以使用errors.Is(err, ErrorSomethingWentWrong)
来捕获特定的错误。Is
函数通过比较指针来实现这一点。但是为了进行比较,我需要知道用于定义errorString
的变量名,以便我可以使用errors.Is
来捕获它。
例如:
ErrorSomethingWentWrong = errors.New("Just an example!")
func DoSomething() (*Something, error) {
return nil, ErrorSomethingWentWrong
}
我知道返回的错误字符串是"Just an example!"
,但我不知道它是否具有变量名ErrorSomethingWentWrong
:
func checkError() {
if errors.Is(err, ErrorSomethingWentWrong){ // 如何知道这个???
//处理错误
}
}
当我使用errors.Is(err, ErrorSomethingWentWrong)
时,我可以捕获这个错误并处理它。但是在调试时,我看不到errorString
代表ErrorSomethingWentWrong
变量。但是当我不知道变量名是ErrorSomethingWentWrong
时,我需要逆向工程代码或阅读文档才能知道返回的错误是哪个。
那么,如何在调试或反射中获取错误变量名呢?
英文:
When working with go there is a pattern used to define errors and handle them in a (to me) very peculiar way. Often errors are declared like ErrorSomethingWentWrong = errors.New("Just an example!")
. You can use errors.Is(err, ErrorSomethingWentWrong)
to catch that specific error. The Is
function can do this by comparing the pointers. But in order to make the comparison I need to know which variable name is used to define the errorString
so I can use errors.Is
to catch it.
For example:
ErrorSomethingWentWrong = errors.New("Just an example!")
func DoSomething() (*Something, error) {
return nil, ErrorSomethingWentWrong
}
I know a error is returned with the string "Just an example!"
But I don't know it has the variable name ErrorSomethingWentWrong
:
func checkError() {
if errors.Is(err, ErrorSomethingWentWrong){ // how to know this???
//handle error
}
}
When I use errors.Is(err, ErrorSomethingWentWrong)
I can catch this error and handle it. When using debugging I can't see that the errorString
represents the ErrorSomethingWentWrong
variable. But when I don't know that the variable name was ErrorSomethingWentWrong
I need to reverse engineer the code or read the docs to know which error is returned.
So how can you know using debugging or reflection to retrieve the error variable name?
答案1
得分: 1
如果我理解正确,这是你的情况:
你从一个包中收到了一个错误,但没有文档告诉你应该处理哪些错误。你正在进行调试,看到了一个errors.errorString
,但不知道是否有一个全局和公共的错误值可以进行比较。
不幸的是,这是一个值,调试器无法告诉你这是否是一个全局变量,如果是的话是哪一个,因为你没有接收到一个变量,只有一个值。
你可以:
- 阅读你调用的包的源代码,看看返回了哪些错误以及如何处理它们
- 在错误字符串中搜索源代码,这可能有助于确定错误的定义或创建位置。
如果你发现你想处理的特定错误在全局范围内没有定义(并且重新编写包对你来说不可行),那么你无法处理它。这可能发生在某人在函数内部使用了errors.New
(不好的实践)。那么你也可以尝试这些方法:
func handleError(err error) {
if err.Error() == "完整的错误字符串" {}
if strings.Contains(err.Error(), "部分错误字符串") {}
}
但我认为这两种方法都不太好看。
简而言之,这是不可能的,你必须阅读源代码。
英文:
If I understand correctly, this is your scenario:
You are receiving an error from a package, and there is no documentation to provide you with the errors you should be handling. You are debugging, and see an errors.errorString
, but don't know if there is a global and public error value that you can compare to.
Unfortunately, this is a value and the debugger can't tell you if this is a global variable or if so which one, because you don't receive a variable, only a value.
You can:
- read through the source code of the package you are calling and see which errors are being returned and how to handle them
- search the source code for the text in the error string, this could help you pinpoint where the error is defined or created.
If it turns out the specific error you wish to handle is not defined globally (and re-writing the package is not feasible for your case), so you can't handle it. This can happen if somebody uses errors.New
inside a function (bad practice). Then you can also try these:
func handleError(err error) {
if err.Error() == "full error string" {}
if strings.Contains(err.Error(), "partial error string") {}
}
But those are both ugly imho.
TL;DR It's not possible, you have to read the source code.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论