Skip to content

Commit d71dfbd

Browse files
committed
Add error logging and improve error handling in ObtainToken function
1 parent 562c3df commit d71dfbd

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

httpclient/httpclient_auth_bearer_token.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package httpclient
66

77
import (
88
"encoding/json"
9+
"fmt"
910
"net/http"
1011
"time"
1112

@@ -39,21 +40,21 @@ func (c *Client) ObtainToken(log logger.Logger) error {
3940

4041
req, err := http.NewRequest("POST", authenticationEndpoint, nil)
4142
if err != nil {
42-
log.LogError("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")
4344
return err
4445
}
4546
req.SetBasicAuth(c.BearerTokenAuthCredentials.Username, c.BearerTokenAuthCredentials.Password)
4647

4748
resp, err := c.httpClient.Do(req)
4849
if err != nil {
49-
log.LogError("POST", authenticationEndpoint, 0, err, "Failed to make request for token")
50+
log.LogError("authentication_request_error", "POST", authenticationEndpoint, 0, err, "Failed to make request for token")
5051
return err
5152
}
5253
defer resp.Body.Close()
5354

5455
if resp.StatusCode != http.StatusOK {
55-
log.Error("Received non-OK response while obtaining token", zap.Int("StatusCode", resp.StatusCode))
56-
return err
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")
57+
return fmt.Errorf("received non-OK response status: %d", resp.StatusCode)
5758
}
5859

5960
tokenResp := &TokenResponse{}

0 commit comments

Comments
 (0)