Skip to content

Commit

Permalink
Update to return net.Error
Browse files Browse the repository at this point in the history
  • Loading branch information
darylnwk committed Apr 29, 2020
1 parent 10a5a39 commit 1994d03
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
12 changes: 11 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package httpclient

import (
"fmt"
"net"
"net/http"
"time"

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
18 changes: 18 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 1994d03

Please sign in to comment.