Skip to content

Commit cc19ccc

Browse files
committed
Fix error handling in HTTP client
1 parent 8c29186 commit cc19ccc

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

httpclient/httpclient_auth_bearer_token.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,20 @@ func (c *Client) ObtainToken(log logger.Logger) error {
4040

4141
req, err := http.NewRequest("POST", authenticationEndpoint, nil)
4242
if err != nil {
43-
log.LogError("authentication_request_creation_error", "POST", authenticationEndpoint, 0, err, "Failed to create new request for token")
43+
log.LogError("authentication_request_creation_error", "POST", authenticationEndpoint, 0, "", err, "Failed to create new request for token")
4444
return err
4545
}
4646
req.SetBasicAuth(c.BearerTokenAuthCredentials.Username, c.BearerTokenAuthCredentials.Password)
4747

4848
resp, err := c.httpClient.Do(req)
4949
if err != nil {
50-
log.LogError("authentication_request_error", "POST", authenticationEndpoint, 0, err, "Failed to make request for token")
50+
log.LogError("authentication_request_error", "POST", authenticationEndpoint, resp.StatusCode, resp.Status, err, "Failed to make request for token")
5151
return err
5252
}
5353
defer resp.Body.Close()
5454

5555
if resp.StatusCode != http.StatusOK {
56-
log.LogError("token_authentication_failed", "POST", authenticationEndpoint, resp.StatusCode, fmt.Errorf("authentication failed with status code: %d", resp.StatusCode), "Token acquisition attempt resulted in a non-OK response")
56+
log.LogError("token_authentication_failed", "POST", authenticationEndpoint, resp.StatusCode, resp.Status, fmt.Errorf("authentication failed with status code: %d", resp.StatusCode), "Token acquisition attempt resulted in a non-OK response")
5757
return fmt.Errorf("received non-OK response status: %d", resp.StatusCode)
5858
}
5959

httpclient/httpclient_error_response.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func handleAPIErrorResponse(resp *http.Response, log logger.Logger) *APIError {
5757
resp.Request.Method, // method
5858
resp.Request.URL.String(), // url
5959
resp.StatusCode, // statusCode
60+
resp.Status, // status
6061
fmt.Errorf(apiError.Message), // err
6162
apiError.Raw, // raw resp
6263
)
@@ -67,7 +68,15 @@ func handleAPIErrorResponse(resp *http.Response, log logger.Logger) *APIError {
6768
var genericErr map[string]interface{}
6869
if err := json.Unmarshal(bodyBytes, &genericErr); err == nil {
6970
apiError.updateFromGenericError(genericErr)
70-
log.LogError("json_generic_error_detected", resp.Request.Method, resp.Request.URL.String(), resp.StatusCode, fmt.Errorf(apiError.Message), "")
71+
log.LogError(
72+
"json_generic_error_detected", // event
73+
resp.Request.Method, // method
74+
resp.Request.URL.String(), // url
75+
resp.StatusCode, // statusCode
76+
resp.Status, // status
77+
fmt.Errorf(apiError.Message), // err
78+
apiError.Raw, // raw resp
79+
)
7180
return apiError
7281
}
7382
} else if isHTMLResponse(resp) {
@@ -78,6 +87,7 @@ func handleAPIErrorResponse(resp *http.Response, log logger.Logger) *APIError {
7887
resp.Request.Method, // method
7988
resp.Request.URL.String(), // url
8089
resp.StatusCode, // statusCode
90+
resp.Status, // status
8191
fmt.Errorf(apiError.Message), // err
8292
apiError.Raw, // raw resp
8393
)
@@ -86,12 +96,13 @@ func handleAPIErrorResponse(resp *http.Response, log logger.Logger) *APIError {
8696
// Handle other non-JSON responses
8797
apiError.Raw = string(bodyBytes)
8898
log.LogError(
89-
"api_non_json_error", // event
90-
resp.Request.Method, // method
91-
resp.Request.URL.String(), // url
92-
resp.StatusCode, // statusCode
99+
"api_non_json_error", // event
100+
resp.Request.Method, // method
101+
resp.Request.URL.String(), // url
102+
resp.StatusCode, // statusCode
103+
resp.Status, // status
93104
fmt.Errorf("Non-JSON error response received"), // err
94-
apiError.Raw, // raw resp
105+
apiError.Raw, // raw resp
95106
)
96107
return apiError
97108
}

0 commit comments

Comments
 (0)