dongraobei6719
2014-03-31 12:29
浏览 107
已采纳

Golang中检测各种网络错误的便携式方法

I would like to identify what kind of error occurred in the network level. The only way I found was checking the error messages with a regular expression, but now I discovered that this messages can be in different languages (depending on the OS configuration), making it difficult to detect by regular expressions. Is there a better way to do it?

package main

import (
  "github.com/miekg/dns"
  "net"
  "regexp"
)

func main() {
  var c dns.Client
  m := new(dns.Msg)

  m.SetQuestion("3com.br.", dns.TypeSOA)
  _, _, err := c.Exchange(m, "ns1.3com.com.:53")
  checkErr(err)

  m.SetQuestion("example.com.", dns.TypeSOA)
  _, _, err = c.Exchange(m, "idontexist.br.:53")
  checkErr(err)

  m.SetQuestion("acasadocartaocuritiba.blog.br.", dns.TypeSOA)
  _, _, err = c.Exchange(m, "ns7.storedns22.in.:53")
  checkErr(err)
}

func checkErr(err error) {
  if err == nil {
    println("Ok")
  } else if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
    println("Timeout")
  } else if match, _ := regexp.MatchString(".*lookup.*", err.Error()); match {
    println("Unknown host")
  } else if match, _ := regexp.MatchString(".*connection refused.*", err.Error()); match {
    println("Connection refused")
  } else {
    println("Other error")
  }
}

Result:

$ go run neterrors.go
Timeout
Unknown host
Connection refused

I discover the problem when testing the system in a Windows OS with Portuguese as default language.

[EDIT]

I found a way to do it using the OpError. Here is the checkErr function again with the new approach. If someone has a better solution I will be very glad to known it!

func checkErr(err error) {
  if err == nil {
    println("Ok")
  } else if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
    println("Timeout")
  } else if opError, ok := err.(*net.OpError); ok {
    if opError.Op == "dial" {
      println("Unknown host")
    } else if opError.Op == "read" {
      println("Connection refused")
    }
  }
}

[EDIT2]

Updated after seong answer.

func checkErr(err error) {
  if err == nil {
    println("Ok")
    return

  } else if netError, ok := err.(net.Error); ok && netError.Timeout() {
    println("Timeout")
    return
  }

  switch t := err.(type) {
  case *net.OpError:
    if t.Op == "dial" {
      println("Unknown host")
    } else if t.Op == "read" {
      println("Connection refused")
    }

  case syscall.Errno:
    if t == syscall.ECONNREFUSED {
      println("Connection refused")
    }
  }
}
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douliangbian7323 2014-03-31 13:15
    已采纳

    The net package works closely with your OS. For OS errors, the Go std library uses the pkg syscall. Have a look here: http://golang.org/pkg/syscall/

    The net package can also return syscall.Errno type errors.

    For a simpler code in you checkErr function, you could consider using a type switch (http://golang.org/doc/effective_go.html#type_switch).

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题