Skip to content

Commit 562c3df

Browse files
committed
Refactor error handling and logging in httpclient package
1 parent 8ae67b7 commit 562c3df

File tree

3 files changed

+14
-15
lines changed

3 files changed

+14
-15
lines changed

httpclient/httpclient_error_response.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,53 +34,47 @@ func (e *APIError) Error() string {
3434
return fmt.Sprintf("API Error (Type: %s, Code: %d): %s", e.Type, e.StatusCode, e.Message)
3535
}
3636

37-
// handleAPIErrorResponse attempts to parse the error response from the API and logs using zap logger.
37+
// handleAPIErrorResponse attempts to parse the error response from the API and logs using the zap logger.
3838
func handleAPIErrorResponse(resp *http.Response, log logger.Logger) *APIError {
39-
// Initialize apiError with the HTTP status code and other fields
4039
apiError := &APIError{
4140
StatusCode: resp.StatusCode,
4241
Type: "APIError", // Default error type
4342
Message: "An error occurred", // Default error message
4443
}
4544

46-
// Read the response body
4745
bodyBytes, err := io.ReadAll(resp.Body)
4846
if err != nil {
4947
// Log and return an error if reading the body fails
50-
log.LogError("READ", resp.Request.URL.String(), resp.StatusCode, err, "Failed to read API error response body")
48+
log.LogError("api_response_read_error", "READ", resp.Request.URL.String(), resp.StatusCode, err, "")
5149
return apiError
5250
}
5351

54-
// Attempt to parse the response into a StructuredError
5552
if err := json.Unmarshal(bodyBytes, &apiError); err == nil && apiError.Message != "" {
56-
// Log the structured error with consistency
57-
log.LogError("API", resp.Request.URL.String(), resp.StatusCode, fmt.Errorf(apiError.Message), "")
53+
// Log the structured error
54+
log.LogError("api_structured_error", "API", resp.Request.URL.String(), resp.StatusCode, fmt.Errorf(apiError.Message), "")
5855
return apiError
5956
}
6057

6158
// If structured parsing fails, attempt to parse into a generic error map
6259
var genericErr map[string]interface{}
6360
if err := json.Unmarshal(bodyBytes, &genericErr); err == nil {
64-
// Extract fields from the generic error map and update apiError accordingly
6561
apiError.updateFromGenericError(genericErr)
66-
67-
// Log the error with extracted details consistently
68-
log.LogError("API", resp.Request.URL.String(), resp.StatusCode, fmt.Errorf(apiError.Message), "")
62+
// Log the error with extracted details
63+
log.LogError("api_generic_error", "API", resp.Request.URL.String(), resp.StatusCode, fmt.Errorf(apiError.Message), "")
6964
return apiError
7065
}
7166

7267
// If all parsing attempts fail, log the raw response
73-
log.LogError("API", resp.Request.URL.String(), resp.StatusCode, fmt.Errorf("failed to parse API error response"), string(bodyBytes))
68+
log.LogError("api_unexpected_error", "API", resp.Request.URL.String(), resp.StatusCode, fmt.Errorf("failed to parse API error response"), string(bodyBytes))
7469
return apiError
7570
}
7671

77-
// updateFromGenericError updates the APIError fields based on a generic error map
7872
func (e *APIError) updateFromGenericError(genericErr map[string]interface{}) {
7973
if msg, ok := genericErr["message"].(string); ok {
8074
e.Message = msg
8175
}
8276
if detail, ok := genericErr["detail"].(string); ok {
8377
e.Detail = detail
8478
}
85-
// Add more fields if necessary
79+
// Add more fields as needed
8680
}

httpclient/httpclient_mocklogger.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ func (m *MockLogger) LogError(event string, method string, url string, statusCod
8181
m.Called(event, method, url, statusCode, err, stacktrace)
8282
}
8383

84+
func (m *MockLogger) LogAuthTokenError(event string, method string, url string, statusCode int, err error) {
85+
m.Called(event, method, url, statusCode, err)
86+
}
87+
8488
func (m *MockLogger) LogRetryAttempt(event string, method string, url string, attempt int, reason string, waitDuration time.Duration, err error) {
8589
m.Called(event, method, url, attempt, reason, waitDuration, err)
8690
}

httpclient/httpclient_request.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ func (c *Client) executeRequestWithRetries(method, endpoint string, body, out in
207207
if apiErr := handleAPIErrorResponse(resp, log); apiErr != nil {
208208
err = apiErr
209209
}
210-
log.LogError(method, endpoint, resp.StatusCode, err, status.TranslateStatusCode(resp))
210+
log.LogError("request_error", method, endpoint, resp.StatusCode, err, status.TranslateStatusCode(resp))
211+
211212
break
212213
}
213214
}

0 commit comments

Comments
 (0)