If you want to assert your way down to the original error, the order of the 
structures returned from http.Get is:

if err, ok := err.(*url.Error); ok {
if err, ok := err.Err.(*net.OpError); ok {
if err, ok := err.Err.(*net.DNSError); ok {
fmt.Println("DNS ERROR:", err)
}
}
}

Again though, the action (http.Get) failed regardless of the reason, so you 
would usually the errors all in the same manner. You can't necessarily 
count on a DNS error being permanent either, just look at what happened a 
few weeks ago, when a huge number of major sites couldn't be resolved. 





On Monday, November 28, 2016 at 12:01:18 PM UTC-5, mb.dha...@gmail.com 
wrote:
>
> I'm downloading a large number of urls and the result is expected to have 
> failure reasons, DNS lookup failed being one.
>
> calling lookup manually would solve my issue, I suppose. Just wanted to 
> make sure that I'm not missing any obvious solutions.
>
> 1. lookup DNS and if it succeeds, 
> 2. try http.Get - which calles Lookup again (also maybe DNS server went 
> down now - improbable but not impossible?)
>
>
> On Monday, November 28, 2016 at 1:05:25 PM UTC+1, Dave Cheney wrote:
>>
>> What specifically do you want to handle about DNS errors ?
>>
>> If you have a set of possible dns names and you don't know if some of 
>> them are valid, you could use net.LookupHost to check which ones are valid 
>> before calling http.Get.
>>
>> On Monday, 28 November 2016 22:44:37 UTC+11, mb.dha...@gmail.com wrote:
>>>
>>> But AFIU, err.Temporary() could be true for other kind of errors that 
>>> might happen, isn't that a problem if I only want to handle DNS errors?
>>>
>>>
>>> On Friday, November 25, 2016 at 6:35:59 PM UTC+1, Victor Vrantchan wrote:
>>>>
>>>> In Go, you can check if a type satisfies a particular behavior by 
>>>> declaring an interface, and then checking if the type satisfies that 
>>>> interface. 
>>>> In your case, you can check if the error is `Temporrary()` or not. 
>>>>
>>>> See this snippet as an example: https://I don't 
>>>> thinplay.golang.org/p/Ffyg61iDpB <https://play.golang.org/p/Ffyg61iDpB>
>>>>
>>>> I recommend 
>>>> https://dave.cheney.net/2016/04/27/dont-just-check-errors-handle-them-gracefully
>>>>  
>>>> and.or the GopherCon talk https://www.youtube.com/watch?v=lsBF58Q-DnY 
>>>> by Dave Cheney for a more in-depth presentation of this idea. 
>>>>
>>>> On Friday, November 25, 2016 at 12:00:06 PM UTC-5, mb.dha...@gmail.com 
>>>> wrote:
>>>>>
>>>>> This is a question about the net/http API, Googling was not very 
>>>>> helpful.
>>>>>
>>>>>     _, err := http.Get("https://random_non_existing_domain.com";)
>>>>>
>>>>>
>>>>> Following code will obviously fail, but how can I check if it failed 
>>>>> because of DNS error not because a billion other things that might have 
>>>>> gone wrong?
>>>>>
>>>>> I tried this, (mostly as a shot in the dark)
>>>>>
>>>>>         if e, ok := err.(net.Error); ok {
>>>>>             fmt.Println("dial error", e.Temporary())
>>>>>         }
>>>>>         if e, ok := err.(*net.AddrError); ok {
>>>>>             fmt.Println("addr rror", e.Temporary())
>>>>>         }
>>>>>         if e, ok := err.(*net.DNSError); ok {
>>>>>             fmt.Println(e)
>>>>>
>>>>> and it seems to print 
>>>>>
>>>>>        dial error false
>>>>>
>>>>> Is there any easy way to do what I'm trying to achieve?
>>>>>
>>>>> -
>>>>> dbalan
>>>>> @notmycommit
>>>>>
>>>>>
>>>>>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to