errors.Is()函数无法正常工作。

huangapple go评论104阅读模式
英文:

errors.Is() doesn't function propertly

问题

我贴了一段代码,本来应该捕获AllTopologyNodesDownError错误,但是它没有起作用,我不知道为什么。

  1. func (sc *ServerConfig) addNodesToCluster(store *ravendb.DocumentStore) error {
  2. clusterTopology, err := sc.getClusterTopology(store)
  3. if errors.Is(err, &ravendb.AllTopologyNodesDownError{}) {
  4. for _, url := range sc.Url.List {
  5. err = addNodeToCluster(store, url)
  6. if err != nil {
  7. return err
  8. }
  9. }
  10. } else if err != nil {
  11. return err
  12. }
  13. }

ravendb.AllTopologyNodesDownError的结构如下:

  1. // AllTopologyNodesDownError represents "all topology nodes are down" error
  2. type AllTopologyNodesDownError struct {
  3. errorBase
  4. }
  5. type errorBase struct {
  6. wrapped error
  7. ErrorStr string
  8. }

调试代码时出现错误的截图

英文:

I pasted a section of code that was supposed to catch an AllTopologyNodesDownError error which doesn't work and I have no idea why.

  1. func (sc *ServerConfig) addNodesToCluster(store *ravendb.DocumentStore) error {
  2. clusterTopology, err := sc.getClusterTopology(store)
  3. if errors.Is(err, &ravendb.AllTopologyNodesDownError{}) {
  4. for _, url := range sc.Url.List {
  5. err = addNodeToCluster(store, url)
  6. if err != nil {
  7. return err
  8. }
  9. }
  10. } else if err != nil {
  11. return err
  12. }

the structure of the ravendb.AllTopologyNodesDownError is

  1. // AllTopologyNodesDownError represents "all topology nodes are down" error
  2. type AllTopologyNodesDownError struct {
  3. errorBase
  4. }
  5. type errorBase struct {
  6. wrapped error
  7. ErrorStr string
  8. }

screen shot of the error when debugging the code

答案1

得分: 0

errors.Is() 用于判断链中的任何错误是否与提供的错误实例相同,但在这种情况下不可能发生,因为您提供了一个字面量的错误类型,其他代码不可能持有该实例或对其的引用。

您的错误看起来像是一个类型,要判断链中的任何错误是否为给定类型,您应该使用 errors.As()

  1. clusterTopology, err := sc.getClusterTopology(store)
  2. var errAllDown *AllTopologyNodesDownError
  3. if errors.As(err, &errAllDown) {
  4. // err 在其链中有一个 *AllTopologyNodesDownError
  5. // errAllDown 现在包含它。
  6. }

  1. 可以通过实现 Unwrap() 接口来覆盖此行为,但您的错误类型没有实现该接口。
英文:

errors.Is() is used to tell if any error in the chain is the same instance as the provided error<sup>1</sup>, that can never be the case here because you provided a literal of your error type, no other code could hold that instance or a reference to it.

Your error looks like a type, to tell if any error in the chain is a given type you should use errors.As():

  1. clusterTopology, err := sc.getClusterTopology(store)
  2. var errAllDown *AllTopologyNodesDownError
  3. if errors.As(err, &amp;errAllDown) {
  4. // err had an *AllTopologyNodesDownError in its
  5. // chain and errAllDown now contains it.
  6. }

<hr>

  1. Can be overridden by implementing the Unwrap() interface which your error type does not.

huangapple
  • 本文由 发表于 2021年9月27日 18:48:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/69345334.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定