如何查找Go标准库函数可能的错误值?

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

How do I find out about possible error values of Go standard library functions?

问题

当调用返回错误的Go函数时,我想知道如何处理非nil的错误值。我可以选择中止程序、记录错误,或将错误传递给调用者,或者采取这些方式的组合。但是,如果我能找出出了什么问题,并以更细粒度的方式做出反应,那将更好。

因此,我如何了解可能的错误值及其含义呢?例如,我想使用http.NewRequest函数。在文档中查找链接。在那里,它只说可能存在错误条件,但没有具体说明是哪些错误。我该如何了解这些错误呢?

英文:

When calling a Go function that returns an error, I wonder how to deal with a non-nil error value. I can just abort, log it, or pass it to the caller. Or a combination thereof. But it would be better if I found out what went wrong and reacted in a more fine-grained manner.

Thus, how can I find out about possible error values and their meanings? For example, I want to use the http.NewRequest function. Look it up in the docs. There, it only says that there are possible error conditions but not which ones. How can I find out about these?

答案1

得分: 0

首先,如果你还没有阅读过的话,我建议你阅读一下关于Go语言错误处理的这篇博客文章,它可能会为你澄清一些问题。

至于查找Go标准库函数可能的错误值,如果没有明确记录,一种方法是查看源代码。例如,在http.NewRequest函数中,你可以点击头部,跳转到源代码。在那里,你可以看到目前只有一种情况下会返回错误,即URL解析失败。探索url.Parse函数,你会发现它可能返回url.Error,你可以通过类型断言进行检查:

package main

import (
	"log"
	"net/http"
	"net/url"
)

func main() {
	r, err := http.NewRequest("GET", "http://[fe80::%31%25en0]:8080/", nil)
	if perr, ok := err.(*url.Error); ok && perr != nil {
		log.Fatal("解析错误,请检查URL格式:", perr)
	} else if err != nil {
		log.Fatal("创建HTTP请求时发生错误")
	}
	
	// ... 成功的情况
	log.Println(r)
}

运行这段代码会得到解析错误,你可以根据需要以不同的方式处理:

2009/11/10 23:00:00 解析错误,请检查URL格式:parse http://[fe80::%31%25en0]:8080/: percent-encoded characters in host

然而,我同意JimB的评论,特别是在这种情况下,具体检查错误类型可能并不是一个有用的做法。在99.99%的情况下,你只需要知道创建请求失败了(err != nil),并以适当的方式处理这种情况(记录日志、返回等)。

英文:

First off, if you haven't read it yet, I recommend reading this blog article about error handling in Go, it might clarify some things for you.

As for finding out the possible error values of Go stdlib functions, if it's not explicitly documented, one option is to go read the code. For example, on the http.NewRequest function, you can click on the header, which takes you to the source code. There you can see that right now, it only returns an error in one case, when the URL parsing fails. Exploring the url.Parse function, you'll see that it may return url.Error, which you can check for with a type assertion:

package main

import (
	"log"
	"net/http"
	"net/url"
)

func main() {
	r, err := http.NewRequest("GET", "http://[fe80::%31%25en0]:8080/", nil)
	if perr, ok := err.(*url.Error); ok && perr != nil {
		log.Fatal("Parse error, check URL formatting: ", perr)
	} else if err != nil {
		log.Fatal("Error creating HTTP request")
	}
	
	// ... success case
	log.Println(r)
}

Running this gives you the parse error, which you may or may not want to handle differently in your application:

2009/11/10 23:00:00 Parse error, check URL formatting: parse http://[fe80::%31%25en0]:8080/: percent-encoded characters in host

I agree with JimB's comment however, specifically checking the type of error in this case is probably not a useful thing to do. In 99.99% of cases all you need to know is that creating the request failed (err != nil) and handle that eventuality in an appropriate way (logging, returning, etc).

huangapple
  • 本文由 发表于 2016年2月6日 05:21:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/35233749.html
匿名

发表评论

匿名网友

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

确定