How to unwrap url.Error returned from http.Client?

拟墨画扇 提交于 2021-01-28 02:00:33

问题


I'm using net.http.client

After sending the request with

resp, err := Client.Do(req)

I'm getting the error I want to work with.

err.Error() returns an error as string. But I need to work with error as an object. I found the method Unwrap() that seems to be returning an url.Error object, but I'm getting err.Unwrap undefined (type error has no field or method Unwrap)

Sorry for dumb question, I'm totally new to golang.


回答1:


According to the documentation, any error returned from Client.Do will be *url.Error, but since the method signature says (*Response, error), you will have to convert it explicitly before use:

urlErr := err.(*url.Error)
if urlErr.Timeout() {
    // ..
}


来源:https://stackoverflow.com/questions/60150236/how-to-unwrap-url-error-returned-from-http-client

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!