@@ -121,39 +121,32 @@ func (c *Client) prepareRequest(method, endpoint string, body interface{}, log l
121
121
// - The method is designed to be called immediately after receiving an HTTP response, serving as a central point for handling all outcomes
122
122
// of an API call.
123
123
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
126
125
127
- // First, check the response status code to determine how to proceed
126
+ // Check for successful response first
128
127
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
130
129
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
133
131
return err
134
132
}
135
133
136
- // Log successful response
137
- log .Info ("HTTP request succeeded" , zap .Int ("status_code" , resp .StatusCode ))
134
+ // If successful, no further action is needed
138
135
return nil
139
136
}
140
137
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
146
144
}
147
145
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 )
157
150
}
158
151
159
152
// retryableHTTPMethods returns a map of HTTP methods that are considered suitable for retrying in case of errors.
0 commit comments