Skip to content

Commit 41b2748

Browse files
authored
Merge pull request #56 from deploymenttheory/dev
Refactor error handling in HTTP client
2 parents 49bd0cd + 97d78f0 commit 41b2748

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

httpclient/httpclient_request.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,8 @@ func (c *Client) executeRequest(method, endpoint string, body, out interface{},
292292
// Check for successful status code
293293
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
294294
// Handle error responses
295-
return nil, c.handleErrorResponse(resp, log, "Failed to process the HTTP request", method, endpoint)
295+
//return nil, c.handleErrorResponse(resp, log, "Failed to process the HTTP request", method, endpoint)
296+
return nil, c.handleErrorResponse(resp, out, log, method, endpoint)
296297
} else {
297298
// Handle successful responses
298299
return resp, c.handleSuccessResponse(resp, out, log, method, endpoint)
@@ -349,7 +350,7 @@ func (c *Client) executeHTTPRequest(req *http.Request, log logger.Logger, method
349350
//
350351
// Returns:
351352
// - An error object parsed from the HTTP response, indicating the nature of the failure.
352-
func (c *Client) handleErrorResponse(resp *http.Response, log logger.Logger, errorMessage, method, endpoint string) error {
353+
func (c *Client) handleErrorResponseV1(resp *http.Response, log logger.Logger, errorMessage, method, endpoint string) error {
353354
apiErr := errors.HandleAPIError(resp, log)
354355

355356
// Log the provided error message along with method, endpoint, and status code.
@@ -363,6 +364,23 @@ func (c *Client) handleErrorResponse(resp *http.Response, log logger.Logger, err
363364
return apiErr
364365
}
365366

367+
func (c *Client) handleErrorResponse(resp *http.Response, out interface{}, log logger.Logger, method, endpoint string) error {
368+
if err := c.APIHandler.HandleResponse(resp, out, log); err != nil {
369+
log.Error("Failed to unmarshal HTTP response",
370+
zap.String("method", method),
371+
zap.String("endpoint", endpoint),
372+
zap.Error(err),
373+
)
374+
return err
375+
}
376+
log.Info("HTTP request succeeded",
377+
zap.String("method", method),
378+
zap.String("endpoint", endpoint),
379+
zap.Int("status_code", resp.StatusCode),
380+
)
381+
return nil
382+
}
383+
366384
// handleSuccessResponse unmarshals a successful HTTP response into the provided output parameter and logs the
367385
// success details. It's designed for use when the response indicates success (status code within 200-299).
368386
// The function logs the request's success and, in case of unmarshalling errors, logs the failure and returns the error.
@@ -462,7 +480,8 @@ func (c *Client) DoMultipartRequest(method, endpoint string, fields map[string]s
462480
// Check for successful status code
463481
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
464482
// Handle error responses
465-
return nil, c.handleErrorResponse(resp, log, "Failed to process the HTTP request", method, endpoint)
483+
//return nil, c.handleErrorResponse(resp, log, "Failed to process the HTTP request", method, endpoint)
484+
return nil, c.handleErrorResponse(resp, out, log, method, endpoint)
466485
} else {
467486
// Handle successful responses
468487
return resp, c.handleSuccessResponse(resp, out, log, method, endpoint)

0 commit comments

Comments
 (0)