|
7 | 7 | "fmt" |
8 | 8 | "io" |
9 | 9 | "net/http" |
| 10 | + "runtime/debug" |
10 | 11 | "strings" |
11 | 12 |
|
12 | 13 | "github.com/deploymenttheory/go-api-http-client/logger" |
@@ -45,36 +46,57 @@ func handleAPIErrorResponse(resp *http.Response, log logger.Logger) *APIError { |
45 | 46 |
|
46 | 47 | bodyBytes, err := io.ReadAll(resp.Body) |
47 | 48 | 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.") |
49 | 49 | return apiError |
50 | 50 | } |
51 | 51 |
|
| 52 | + stackTrace := string(debug.Stack()) |
| 53 | + |
52 | 54 | // Check if the response is JSON |
53 | 55 | if isJSONResponse(resp) { |
54 | 56 | // Attempt to parse the response into a StructuredError |
55 | 57 | 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 | + ) |
57 | 67 | return apiError |
58 | 68 | } |
59 | 69 |
|
60 | 70 | // If structured parsing fails, attempt to parse into a generic error map |
61 | 71 | var genericErr map[string]interface{} |
62 | 72 | if err := json.Unmarshal(bodyBytes, &genericErr); err == nil { |
63 | 73 | 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), "") |
65 | 75 | return apiError |
66 | 76 | } |
67 | 77 | } else if isHTMLResponse(resp) { |
68 | 78 | // Handle HTML response |
69 | 79 | 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 | + ) |
72 | 88 | return apiError |
73 | 89 | } else { |
74 | 90 | // Handle other non-JSON responses |
75 | 91 | 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 | + ) |
78 | 100 | return apiError |
79 | 101 | } |
80 | 102 |
|
|
0 commit comments