From 2de1cd18c05a779b27f68adfb9d60a1277bb6d55 Mon Sep 17 00:00:00 2001 From: jwijenbergh Date: Tue, 6 Feb 2024 14:09:45 +0100 Subject: i18nerr + HTTP: Properly convert timeout errors --- internal/http/http.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'internal') 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 -- cgit v1.2.3