Alex
Alex

Reputation: 1021

How can I check specific golang net/http error code?

package main

import "net/http"

func main() {
     req, err := http.NewRequest("GET", "http://domain_does_not_exist", nil)
     if err != nil { panic("NewRequest") }
     client := http.Client{ }
     _, err = client.Do(req)
     if err == ???
}

I would like to check my GET request for specific error(DNS resolve error). How to accomplish this?

Upvotes: 1

Views: 2693

Answers (1)

Toni Cárdenas
Toni Cárdenas

Reputation: 6820

Package "errors" has functions As, Is to unwrap specific error types, and package "net" has a *DNSError type. So:

var dnsErr *net.DNSError
if errors.As(err, &dnsErr) {
    ...
}

Upvotes: 5

Related Questions