From 1994d0370e6e3d6f193fa5af90dc82a5bfd9dd61 Mon Sep 17 00:00:00 2001 From: Daryl Ng Date: Wed, 29 Apr 2020 19:56:53 +0800 Subject: [PATCH] Update to return net.Error --- client.go | 12 +++++++++++- error.go | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 error.go diff --git a/client.go b/client.go index a2852c3..2b377e5 100644 --- a/client.go +++ b/client.go @@ -2,6 +2,7 @@ package httpclient import ( "fmt" + "net" "net/http" "time" @@ -45,6 +46,7 @@ func NewClient(opts ...Option) *Client { func (client *Client) Do(request *http.Request) (*http.Response, error) { var ( response *http.Response + timeout bool success, errs = client.Retryer.Do(func() error { var err error @@ -69,7 +71,15 @@ func (client *Client) Do(request *http.Request) (*http.Response, error) { ) if !success { - return response, fmt.Errorf("httpclient: request occurred with errors: %s", errs) + // Check if last error is a timeout error + if err, ok := errs[len(errs)-1].(net.Error); ok && err.Timeout() { + timeout = true + } + + return response, &httpError{ + err: fmt.Sprintf("httpclient: request occurred with errors: %s", errs), + timeout: timeout, + } } return response, nil diff --git a/error.go b/error.go new file mode 100644 index 0000000..7faad92 --- /dev/null +++ b/error.go @@ -0,0 +1,18 @@ +package httpclient + +type httpError struct { + err string + timeout bool +} + +func (e *httpError) Error() string { + return e.err +} + +func (e *httpError) Timeout() bool { + return e.timeout +} + +func (e *httpError) Temporary() bool { + return true +}