简体   繁体   English

如何知道遵循 go 错误的变量名

[英]How to know variable name adhering to go error

When working with go there is a pattern used to define errors and handle them in a (to me) very peculiar way.使用 go 时,有一种模式用于定义错误并以(对我而言)非常奇特的方式处理它们。 Often errors are declared like ErrorSomethingWentWrong = errors.New("Just an example!") .通常错误被声明为ErrorSomethingWentWrong = errors.New("Just an example!") You can use errors.Is(err, ErrorSomethingWentWrong) to catch that specific error.您可以使用errors.Is(err, ErrorSomethingWentWrong)来捕获该特定错误。 The Is function can do this by comparing the pointers. Is function 可以通过比较指针来做到这一点。 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.但是为了进行比较,我需要知道哪个变量名用于定义errorString以便我可以使用errors.Is来捕获它。

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!"我知道会返回一个错误,字符串为"Just an example!" But I don't know it has the variable name ErrorSomethingWentWrong :但我不知道它有变量名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.当我使用errors.Is(err, ErrorSomethingWentWrong)时,我可以捕获并处理这个错误。 When using debugging I can't see that the errorString represents the ErrorSomethingWentWrong variable.使用调试时,我看不到errorString表示ErrorSomethingWentWrong变量。 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.但是当我不知道变量名称是ErrorSomethingWentWrong ,我需要对代码进行逆向工程或阅读文档以了解返回了哪个错误。

在此处输入图像描述

So how can you know using debugging or reflection to retrieve the error variable name?那么你怎么知道使用调试或反射来检索错误变量名呢?

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.您收到来自 package 的错误,并且没有文档向您提供您应该处理的错误。 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.您正在调试,并看到errors.errorString ,但不知道是否存在可以比较的全局和公共错误值。

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通读你正在调用的 package 的源代码,看看返回了哪些错误以及如何处理它们
  • 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.如果事实证明您希望处理的特定错误未在全局定义(并且重写 package 对您的情况不可行),那么您将无法处理它。 This can happen if somebody uses errors.New inside a function (bad practice).如果有人在 function 中使用errors.New (错误做法),就会发生这种情况。 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. TL;DR这是不可能的,你必须阅读源代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM