Since the Timeout method sets the dealines for dial, read, and write, you can use os.IsTimeout (all error types in the net and net/url packages implement Timeout() bool
). Contexts are not supported by gorequest, so context.Canceled doesn't have to be considered:
package main
import (
"log"
"net/http"
"net/http/httptest"
"time"
"github.com/parnurzeal/gorequest"
)
func main() {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(time.Second)
}))
request := gorequest.New()
_, _, errs := request.Get(s.URL).Timeout(500 * time.Millisecond).End()
for _, err := range errs {
if os.IsTimeout(err) {
log.Fatal("timeout")
}
}
}