Skip to content

Commit 57a722d

Browse files
authored
Merge pull request #31 from deploymenttheory/dev
Refactor error handling in processResponse function
2 parents 6c8ef0b + df200fd commit 57a722d

File tree

1 file changed

+15
-22
lines changed

1 file changed

+15
-22
lines changed

httpclient/httpclient_request.go

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -121,39 +121,32 @@ func (c *Client) prepareRequest(method, endpoint string, body interface{}, log l
121121
// - The method is designed to be called immediately after receiving an HTTP response, serving as a central point for handling all outcomes
122122
// of an API call.
123123
func (c *Client) processResponse(resp *http.Response, out interface{}, log logger.Logger) error {
124-
// Ensure the response body is closed only after this function completes
125-
defer resp.Body.Close()
124+
defer resp.Body.Close() // Ensure the response body is always closed
126125

127-
// First, check the response status code to determine how to proceed
126+
// Check for successful response first
128127
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
129-
// For successful responses, attempt to unmarshal the response body
128+
// Delegate the unmarshaling of the response body to the API handler
130129
if err := c.APIHandler.UnmarshalResponse(resp, out, log); err != nil {
131-
// Log and return errors encountered during unmarshaling
132-
log.Error("Failed to unmarshal HTTP response", zap.Error(err))
130+
// Errors would have been logged by the API handler, so just return the error
133131
return err
134132
}
135133

136-
// Log successful response
137-
log.Info("HTTP request succeeded", zap.Int("status_code", resp.StatusCode))
134+
// If successful, no further action is needed
138135
return nil
139136
}
140137

141-
// For non-success responses, check for API errors or other issues
142-
if apiErr := c.APIHandler.UnmarshalResponse(resp, out, log); apiErr != nil {
143-
// Even if unmarshaling into the output fails, we might still want to capture the error
144-
log.Error("Received an API error or failed to unmarshal error response", zap.Int("status_code", resp.StatusCode), zap.Error(apiErr))
145-
return apiErr
138+
// For non-success responses, delegate error handling to the API handler as well
139+
// This might involve logging and constructing a detailed error from the response body
140+
if err := c.APIHandler.UnmarshalResponse(resp, out, log); err != nil {
141+
// Since UnmarshalResponse handles both successful and error responses,
142+
// it might already log and return an appropriate error, so just return it here
143+
return err
146144
}
147145

148-
// If we reach this point, the response was not successful, but did not contain a recognizable API error
149-
statusDescription := errors.TranslateStatusCode(resp.StatusCode)
150-
errorMessage := fmt.Sprintf("HTTP request failed with status code %d - %s", resp.StatusCode, statusDescription)
151-
152-
// Log the error message
153-
log.Error(errorMessage, zap.Int("status_code", resp.StatusCode), zap.String("description", statusDescription))
154-
155-
// Return a generic error with the status code and description
156-
return fmt.Errorf(errorMessage)
146+
// If the API handler's UnmarshalResponse method doesn't handle non-success status codes,
147+
// you can log and return a generic error here
148+
log.Error("Received non-success HTTP status code", zap.Int("status_code", resp.StatusCode))
149+
return fmt.Errorf("HTTP request failed with status code %d", resp.StatusCode)
157150
}
158151

159152
// retryableHTTPMethods returns a map of HTTP methods that are considered suitable for retrying in case of errors.

0 commit comments

Comments
 (0)