Skip to content

Commit 462a51e

Browse files
authored
Merge pull request #101 from deploymenttheory/dev
Dev
2 parents 14a15f2 + cc19ccc commit 462a51e

File tree

6 files changed

+28
-21
lines changed

6 files changed

+28
-21
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: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"fmt"
88
"io"
99
"net/http"
10-
"runtime/debug"
1110
"strings"
1211

1312
"github.com/deploymenttheory/go-api-http-client/logger"
@@ -49,20 +48,18 @@ func handleAPIErrorResponse(resp *http.Response, log logger.Logger) *APIError {
4948
return apiError
5049
}
5150

52-
stackTrace := string(debug.Stack())
53-
5451
// Check if the response is JSON
5552
if isJSONResponse(resp) {
5653
// Attempt to parse the response into a StructuredError
5754
if err := json.Unmarshal(bodyBytes, &apiError); err == nil && apiError.Message != "" {
58-
stackTrace := string(debug.Stack())
5955
log.LogError(
6056
"json_structured_error_detected", // event
6157
resp.Request.Method, // method
6258
resp.Request.URL.String(), // url
6359
resp.StatusCode, // statusCode
60+
resp.Status, // status
6461
fmt.Errorf(apiError.Message), // err
65-
stackTrace, // stacktrace
62+
apiError.Raw, // raw resp
6663
)
6764
return apiError
6865
}
@@ -71,7 +68,15 @@ func handleAPIErrorResponse(resp *http.Response, log logger.Logger) *APIError {
7168
var genericErr map[string]interface{}
7269
if err := json.Unmarshal(bodyBytes, &genericErr); err == nil {
7370
apiError.updateFromGenericError(genericErr)
74-
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+
)
7580
return apiError
7681
}
7782
} else if isHTMLResponse(resp) {
@@ -82,20 +87,22 @@ func handleAPIErrorResponse(resp *http.Response, log logger.Logger) *APIError {
8287
resp.Request.Method, // method
8388
resp.Request.URL.String(), // url
8489
resp.StatusCode, // statusCode
90+
resp.Status, // status
8591
fmt.Errorf(apiError.Message), // err
86-
stackTrace, // stacktrace
92+
apiError.Raw, // raw resp
8793
)
8894
return apiError
8995
} else {
9096
// Handle other non-JSON responses
9197
apiError.Raw = string(bodyBytes)
9298
log.LogError(
93-
"api_non_json_error", // event
94-
resp.Request.Method, // method
95-
resp.Request.URL.String(), // url
96-
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
97104
fmt.Errorf("Non-JSON error response received"), // err
98-
stackTrace, // stacktrace
105+
apiError.Raw, // raw resp
99106
)
100107
return apiError
101108
}

httpclient/httpclient_mocklogger.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ func (m *MockLogger) LogRequestEnd(event string, method string, url string, stat
7777
m.Called(event, method, url, statusCode, duration)
7878
}
7979

80-
func (m *MockLogger) LogError(event string, method string, url string, statusCode int, err error, stacktrace string) {
81-
m.Called(event, method, url, statusCode, err, stacktrace)
80+
func (m *MockLogger) LogError(event string, method string, url string, statusCode int, serverStatusMessage string, err error, stacktrace string) {
81+
m.Called(event, method, url, statusCode, serverStatusMessage, err, stacktrace)
8282
}
8383

8484
func (m *MockLogger) LogAuthTokenError(event string, method string, url string, statusCode int, err error) {

httpclient/httpclient_request.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,7 @@ 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("request_error", method, endpoint, resp.StatusCode, err, status.TranslateStatusCode(resp))
211-
210+
log.LogError("request_error", method, endpoint, resp.StatusCode, resp.Status, err, status.TranslateStatusCode(resp))
212211
break
213212
}
214213
}

logger/zaplogger_logfields.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (d *defaultLogger) LogRequestEnd(event string, method string, url string, s
3636
}
3737

3838
// LogError logs an error that occurs during the processing of an HTTP request or any other event, if the current log level permits.
39-
func (d *defaultLogger) LogError(event string, method, url string, statusCode int, err error, stacktrace string) {
39+
func (d *defaultLogger) LogError(event string, method, url string, statusCode int, serverStatusMessage string, err error, stacktrace string) {
4040
if d.logLevel <= LogLevelError {
4141
errorMessage := ""
4242
if err != nil {
@@ -48,6 +48,7 @@ func (d *defaultLogger) LogError(event string, method, url string, statusCode in
4848
zap.String("method", method),
4949
zap.String("url", url),
5050
zap.Int("status_code", statusCode),
51+
zap.String("status_message", serverStatusMessage),
5152
zap.String("error_message", errorMessage),
5253
zap.String("stacktrace", stacktrace),
5354
}

logger/zaplogger_logger.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type Logger interface {
3434
// Updated method signatures to include the 'event' parameter
3535
LogRequestStart(event string, requestID string, userID string, method string, url string, headers map[string][]string)
3636
LogRequestEnd(event string, method string, url string, statusCode int, duration time.Duration)
37-
LogError(event string, method string, url string, statusCode int, err error, stacktrace string)
37+
LogError(event string, method string, url string, statusCode int, serverStatusMessage string, err error, stacktrace string)
3838
LogAuthTokenError(event string, method string, url string, statusCode int, err error)
3939
LogRetryAttempt(event string, method string, url string, attempt int, reason string, waitDuration time.Duration, err error)
4040
LogRateLimiting(event string, method string, url string, retryAfter string, waitDuration time.Duration)

0 commit comments

Comments
 (0)