Skip to content

Commit 3f705d7

Browse files
committed
Refactor HTTP error handling and request execution***
1 parent ab42ce2 commit 3f705d7

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

errors/http_error_handling.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func TranslateStatusCode(resp *http.Response) string {
124124
if message, exists := messages[resp.StatusCode]; exists {
125125
return message
126126
}
127-
return "An unexpected error occurred. Please try again later."
127+
return fmt.Sprintf("Unknown status code: %d", resp.StatusCode)
128128
}
129129

130130
// IsNonRetryableStatusCode checks if the provided response indicates a non-retryable error.

httpclient/httpclient_request.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,16 @@ func (c *Client) executeRequestWithRetries(method, endpoint string, body, out in
157157
totalRetryDeadline := time.Now().Add(c.clientConfig.ClientOptions.TotalRetryDuration)
158158

159159
var resp *http.Response
160-
retryCount := 0
160+
var retryCount int
161161
for time.Now().Before(totalRetryDeadline) { // Check if the current time is before the total retry deadline
162162
req = req.WithContext(ctx)
163-
resp, err = c.executeHTTPRequest(req, log, method, endpoint)
163+
resp, err = c.do(req, log, method, endpoint)
164164
// Check for successful status code
165-
if err == nil && resp.StatusCode >= 200 && resp.StatusCode < 300 {
165+
if err == nil && resp.StatusCode >= 200 && resp.StatusCode < 400 {
166+
if resp.StatusCode >= 300 {
167+
log.Warn("Redirect response received", zap.Int("status_code", resp.StatusCode), zap.String("location", resp.Header.Get("Location")))
168+
}
169+
// Handle the response as successful, even if it's a redirect.
166170
return resp, c.handleSuccessResponse(resp, out, log, method, endpoint)
167171
}
168172

@@ -298,23 +302,25 @@ func (c *Client) executeRequest(method, endpoint string, body, out interface{})
298302
req = req.WithContext(ctx)
299303

300304
// Execute the HTTP request
301-
resp, err := c.executeHTTPRequest(req, log, method, endpoint)
305+
resp, err := c.do(req, log, method, endpoint)
302306
if err != nil {
303307
return nil, err
304308
}
305309

306310
// Checks for the presence of a deprecation header in the HTTP response and logs if found.
307311
CheckDeprecationHeader(resp, log)
308312

309-
// Check for successful status code
310-
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
311-
// Handle error responses
312-
//return nil, c.handleErrorResponse(resp, log, "Failed to process the HTTP request", method, endpoint)
313-
return nil, c.handleErrorResponse(resp, out, log, method, endpoint)
314-
} else {
315-
// Handle successful responses
313+
// Check for successful status code, including redirects
314+
if resp.StatusCode >= 200 && resp.StatusCode < 400 {
315+
// Warn on redirects but proceed as successful
316+
if resp.StatusCode >= 300 {
317+
log.Warn("Redirect response received", zap.Int("status_code", resp.StatusCode), zap.String("location", resp.Header.Get("Location")))
318+
}
316319
return resp, c.handleSuccessResponse(resp, out, log, method, endpoint)
317320
}
321+
322+
// Handle error responses for status codes outside the successful range
323+
return nil, c.handleErrorResponse(resp, out, log, method, endpoint)
318324
}
319325

320326
// executeHTTPRequest sends an HTTP request using the client's HTTP client. It logs the request and error details, if any,
@@ -334,7 +340,7 @@ func (c *Client) executeRequest(method, endpoint string, body, out interface{})
334340
// Usage:
335341
// This function should be used whenever the client needs to send an HTTP request. It abstracts away the common logic of
336342
// request execution and error handling, providing detailed logs for debugging and monitoring.
337-
func (c *Client) executeHTTPRequest(req *http.Request, log logger.Logger, method, endpoint string) (*http.Response, error) {
343+
func (c *Client) do(req *http.Request, log logger.Logger, method, endpoint string) (*http.Response, error) {
338344
resp, err := c.httpClient.Do(req)
339345
if err != nil {
340346
// Log the error with structured logging, including method, endpoint, and the error itself
@@ -477,7 +483,7 @@ func (c *Client) DoMultipartRequest(method, endpoint string, fields map[string]s
477483
headerManager.LogHeaders(c)
478484

479485
// Execute the request
480-
resp, err := c.executeHTTPRequest(req, log, method, endpoint)
486+
resp, err := c.do(req, log, method, endpoint)
481487
if err != nil {
482488
return nil, err
483489
}

0 commit comments

Comments
 (0)