diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2024-02-06 14:09:45 +0100 |
|---|---|---|
| committer | Jeroen Wijenbergh <46386452+jwijenbergh@users.noreply.github.com> | 2024-02-19 14:15:07 +0100 |
| commit | 2de1cd18c05a779b27f68adfb9d60a1277bb6d55 (patch) | |
| tree | 4a75d91f430a4b0826ec0f12a503028deaf4f186 /internal/http/http.go | |
| parent | 6f7e32b6d9dcf4732dd0acbd89be77b42805a5ca (diff) | |
i18nerr + HTTP: Properly convert timeout errors
Diffstat (limited to 'internal/http/http.go')
| -rw-r--r-- | internal/http/http.go | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/internal/http/http.go b/internal/http/http.go index 58e8f05..262309a 100644 --- a/internal/http/http.go +++ b/internal/http/http.go @@ -201,8 +201,10 @@ func (c *Client) Do(ctx context.Context, method string, urlStr string, opts *Opt // Do request res, err := c.Client.Do(req) if err != nil { - return nil, nil, errors.WrapPrefix(err, - fmt.Sprintf("failed HTTP request with method %s and url %s", method, urlStr), 0) + if errors.Is(err, context.DeadlineExceeded) { + return nil, nil, &TimeoutError{URL: urlStr, Method: method} + } + return nil, nil, fmt.Errorf("failed HTTP request with method: '%s', url: '%s' and error: %w", method, urlStr, err) } // Request successful, make sure body is closed at the end @@ -229,6 +231,21 @@ func (c *Client) Do(ctx context.Context, method string, urlStr string, opts *Opt return res.Header, body, nil } +// TimeoutError indicates that we have gotten a timeout +type TimeoutError struct { + URL string + Method string +} + +// Error returns the TimeoutError as an error string. +func (e *TimeoutError) Error() string { + return fmt.Sprintf( + "timeout in obtaining HTTP resource: '%s' with method: '%s'", + e.URL, + e.Method, + ) +} + // StatusError indicates that we have received a HTTP status error. type StatusError struct { URL string |
