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

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

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

问题

如果你这样做:

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

你会得到以下输出:

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

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

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

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

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

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

*url.Error

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

英文:

If you do this:

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

you get the output:

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:

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

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

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

fmt.Printf("%T") gives:

*url.Error

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

答案1

得分: 3

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

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

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

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

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()来返回字符串。
并且检查它是否包含特定的键,类似于这样:

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

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

err := http.Get(url)

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

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

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

in your case you can try somthing like:

err := http.Get(url)

if strings.Contains(err.Error(), "unsupported protocol scheme") {
    //add protocol scheme to url string
}

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:

确定