Skip to content

Commit 14a15f2

Browse files
authored
Merge pull request #100 from deploymenttheory/dev
Add stack trace to error logging
2 parents 6e99576 + cc5530b commit 14a15f2

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

httpclient/httpclient_error_response.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"io"
99
"net/http"
10+
"runtime/debug"
1011
"strings"
1112

1213
"github.com/deploymenttheory/go-api-http-client/logger"
@@ -45,36 +46,57 @@ func handleAPIErrorResponse(resp *http.Response, log logger.Logger) *APIError {
4546

4647
bodyBytes, err := io.ReadAll(resp.Body)
4748
if err != nil {
48-
log.LogError("api_response_read_failure", resp.Request.Method, resp.Request.URL.String(), resp.StatusCode, err, "Failed to read the API error response body, which might contain further details about the error.")
4949
return apiError
5050
}
5151

52+
stackTrace := string(debug.Stack())
53+
5254
// Check if the response is JSON
5355
if isJSONResponse(resp) {
5456
// Attempt to parse the response into a StructuredError
5557
if err := json.Unmarshal(bodyBytes, &apiError); err == nil && apiError.Message != "" {
56-
log.LogError("api_structured_error_detected", resp.Request.Method, resp.Request.URL.String(), resp.StatusCode, fmt.Errorf(apiError.Message), "")
58+
stackTrace := string(debug.Stack())
59+
log.LogError(
60+
"json_structured_error_detected", // event
61+
resp.Request.Method, // method
62+
resp.Request.URL.String(), // url
63+
resp.StatusCode, // statusCode
64+
fmt.Errorf(apiError.Message), // err
65+
stackTrace, // stacktrace
66+
)
5767
return apiError
5868
}
5969

6070
// If structured parsing fails, attempt to parse into a generic error map
6171
var genericErr map[string]interface{}
6272
if err := json.Unmarshal(bodyBytes, &genericErr); err == nil {
6373
apiError.updateFromGenericError(genericErr)
64-
log.LogError("api_generic_error_detected", resp.Request.Method, resp.Request.URL.String(), resp.StatusCode, fmt.Errorf(apiError.Message), "")
74+
log.LogError("json_generic_error_detected", resp.Request.Method, resp.Request.URL.String(), resp.StatusCode, fmt.Errorf(apiError.Message), "")
6575
return apiError
6676
}
6777
} else if isHTMLResponse(resp) {
6878
// Handle HTML response
6979
apiError.Raw = string(bodyBytes)
70-
apiError.Message = "HTML error page received"
71-
log.LogError("api_html_error", resp.Request.Method, resp.Request.URL.String(), resp.StatusCode, fmt.Errorf("HTML error page received"), "")
80+
log.LogError(
81+
"api_html_error", // event
82+
resp.Request.Method, // method
83+
resp.Request.URL.String(), // url
84+
resp.StatusCode, // statusCode
85+
fmt.Errorf(apiError.Message), // err
86+
stackTrace, // stacktrace
87+
)
7288
return apiError
7389
} else {
7490
// Handle other non-JSON responses
7591
apiError.Raw = string(bodyBytes)
76-
apiError.Message = "Non-JSON error response received"
77-
log.LogError("api_non_json_error", resp.Request.Method, resp.Request.URL.String(), resp.StatusCode, fmt.Errorf("Non-JSON error response received"), "")
92+
log.LogError(
93+
"api_non_json_error", // event
94+
resp.Request.Method, // method
95+
resp.Request.URL.String(), // url
96+
resp.StatusCode, // statusCode
97+
fmt.Errorf("Non-JSON error response received"), // err
98+
stackTrace, // stacktrace
99+
)
78100
return apiError
79101
}
80102

0 commit comments

Comments
 (0)