如何获取错误类型以在错误处理中使用

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

How to get the type of error to use in error handling

问题

如果你这样做:

  1. _, err := http.Get("google.com")
  2. if err != nil {
  3. log.Fatal(err)
  4. }

你会得到以下输出:

  1. 2021/08/05 15:42:18 Get "google.com": unsupported protocol scheme ""

我想知道如何获取错误的类型,这样我就可以像这样处理错误:

  1. if errors.Is(err, "unsupported protocol scheme") {
  2. // 在 URL 字符串中添加协议方案
  3. }

我尝试了 fmt.Printf("%#v", err),得到的结果是:

  1. &url.Error{Op:"Get", URL:"google.com", Err:(*errors.errorString)(0xc000098c40)}

fmt.Printf("%T") 得到的结果是:

  1. *url.Error

编辑:如果你给我点踩,我真的希望你能留下评论,告诉我你的想法。

英文:

If you do this:

  1. _, err := http.Get("google.com")
  2. if err != nil {
  3. log.Fatal(err)
  4. }

you get the output:

  1. 2021/08/05 15:42:18 Get "google.com": unsupported protocol scheme ""

I'm wondering how do I get the type of error, so I can handle the error like so:

  1. if errors.Is(err, "unsupported protocol scheme") {
  2. //add protocol scheme to url string
  3. }

I've tried fmt.Printf("%#v", err), which gives:

  1. &url.Error{Op:"Get", URL:"google.com", Err:(*errors.errorString)(0xc000098c40)}

fmt.Printf("%T") gives:

  1. *url.Error

Edit: If you're downvoting, I'd really appreciate a comment with your thoughts.

答案1

得分: 3

  1. var e *url.Error
  2. if errors.As(err, &e) && strings.HasPrefix(e.Err.Error(), "unsupported protocol scheme") {
  3. //在URL字符串中添加协议方案
  4. }

请注意,与非标准化字符串进行比较不应被视为具有未来性。例如,如果Go的未来版本决定更改该错误消息的措辞,您的代码将会出错。

尽管Go承诺在不同版本之间保持兼容性,但我认为这个承诺不包括字符串内容。

英文:
  1. var e *url.Error
  2. if errors.As(err, &e) && strings.HasPrefix(e.Err.Error(), "unsupported protocol scheme") {
  3. //add protocol scheme to url string
  4. }

https://play.golang.org/p/VKpMfrBp_EF

Note that comparison against non-standardized strings should not be considered future-proof. For example if a future version of Go decides to change the wording of that error message your code will break.

Although Go does promise compatibility across versions, I don't think that that promise extends to string content.

答案2

得分: 0

你可以使用err.Error()来返回字符串。
并且检查它是否包含特定的键,类似于这样:

  1. if err = db.Ping(); err != nil {
  2. switch {
  3. case strings.Contains(err.Error(), "connection refused"):
  4. // 处理连接被拒绝的情况。例如:
  5. cmd := exec.Command("sudo", "service", "mariadb", "start")
  6. _ = cmd.Run()
  7. default:
  8. log.Println("未知类型的错误", err)
  9. }
  10. }

在你的情况下,你可以尝试类似这样的代码:

  1. err := http.Get(url)
  2. if strings.Contains(err.Error(), "unsupported protocol scheme") {
  3. // 将协议方案添加到url字符串中
  4. }
英文:

you can use err.Error() to return string.
and check if it contiane a spicifice keys, something like this:

  1. if err = db.Ping(); err != nil {
  2. switch {
  3. case strings.Contains(err.Error(), "connection refused"):
  4. // handle connection refused. for example:
  5. cmd := exec.Command("sudo", "service", "mariadb", "start")
  6. _ = cmd.Run()
  7. default:
  8. log.Println("unknown kind of this error", err)
  9. }
  10. }

in your case you can try somthing like:

  1. err := http.Get(url)
  2. if strings.Contains(err.Error(), "unsupported protocol scheme") {
  3. //add protocol scheme to url string
  4. }

huangapple
  • 本文由 发表于 2021年8月5日 14:47:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/68661823.html
匿名

发表评论

匿名网友

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

确定