|
7 | 7 | "fmt" |
8 | 8 | "io" |
9 | 9 | "net/http" |
| 10 | + "strings" |
10 | 11 |
|
11 | 12 | "github.com/deploymenttheory/go-api-http-client/logger" |
12 | 13 | ) |
@@ -44,37 +45,70 @@ func handleAPIErrorResponse(resp *http.Response, log logger.Logger) *APIError { |
44 | 45 |
|
45 | 46 | bodyBytes, err := io.ReadAll(resp.Body) |
46 | 47 | if err != nil { |
47 | | - // Log and return an error if reading the body fails |
48 | | - log.LogError("api_response_read_error", "READ", resp.Request.URL.String(), resp.StatusCode, err, "") |
| 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 | | - if err := json.Unmarshal(bodyBytes, &apiError); err == nil && apiError.Message != "" { |
53 | | - // Log the structured error |
54 | | - log.LogError("api_structured_error", "API", resp.Request.URL.String(), resp.StatusCode, fmt.Errorf(apiError.Message), "") |
55 | | - return apiError |
56 | | - } |
| 52 | + // Check if the response is JSON |
| 53 | + if isJSONResponse(resp) { |
| 54 | + // Attempt to parse the response into a StructuredError |
| 55 | + 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), "") |
| 57 | + return apiError |
| 58 | + } |
57 | 59 |
|
58 | | - // If structured parsing fails, attempt to parse into a generic error map |
59 | | - var genericErr map[string]interface{} |
60 | | - if err := json.Unmarshal(bodyBytes, &genericErr); err == nil { |
61 | | - apiError.updateFromGenericError(genericErr) |
62 | | - // Log the error with extracted details |
63 | | - log.LogError("api_generic_error", "API", resp.Request.URL.String(), resp.StatusCode, fmt.Errorf(apiError.Message), "") |
| 60 | + // If structured parsing fails, attempt to parse into a generic error map |
| 61 | + var genericErr map[string]interface{} |
| 62 | + if err := json.Unmarshal(bodyBytes, &genericErr); err == nil { |
| 63 | + apiError.updateFromGenericError(genericErr) |
| 64 | + log.LogError("api_generic_error_detected", resp.Request.Method, resp.Request.URL.String(), resp.StatusCode, fmt.Errorf(apiError.Message), "") |
| 65 | + return apiError |
| 66 | + } |
| 67 | + } else if isHTMLResponse(resp) { |
| 68 | + // Handle HTML response |
| 69 | + 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"), "") |
| 72 | + return apiError |
| 73 | + } else { |
| 74 | + // Handle other non-JSON responses |
| 75 | + 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"), "") |
64 | 78 | return apiError |
65 | 79 | } |
66 | 80 |
|
67 | | - // If all parsing attempts fail, log the raw response |
68 | | - log.LogError("api_unexpected_error", "API", resp.Request.URL.String(), resp.StatusCode, fmt.Errorf("failed to parse API error response"), string(bodyBytes)) |
69 | 81 | return apiError |
70 | 82 | } |
71 | 83 |
|
| 84 | +// isJSONResponse checks if the response Content-Type indicates JSON |
| 85 | +func isJSONResponse(resp *http.Response) bool { |
| 86 | + contentType := resp.Header.Get("Content-Type") |
| 87 | + return strings.Contains(contentType, "application/json") |
| 88 | +} |
| 89 | + |
| 90 | +// isHTMLResponse checks if the response Content-Type indicates HTML |
| 91 | +func isHTMLResponse(resp *http.Response) bool { |
| 92 | + contentType := resp.Header.Get("Content-Type") |
| 93 | + return strings.Contains(contentType, "text/html") |
| 94 | +} |
| 95 | + |
| 96 | +// updateFromGenericError updates the APIError fields based on a generic error map extracted from an API response. |
| 97 | +// This function is useful for cases where the error response does not match the predefined StructuredError format, |
| 98 | +// and instead, a more generic error handling approach is needed. It extracts known fields such as 'message' and 'detail' |
| 99 | +// from the generic error map and updates the corresponding fields in the APIError instance. |
| 100 | +// |
| 101 | +// Parameters: |
| 102 | +// - genericErr: A map[string]interface{} representing the generic error structure extracted from an API response. |
| 103 | +// |
| 104 | +// The function checks for the presence of 'message' and 'detail' keys in the generic error map. If these keys are present, |
| 105 | +// their values are used to update the 'Message' and 'Detail' fields of the APIError instance, respectively. |
72 | 106 | func (e *APIError) updateFromGenericError(genericErr map[string]interface{}) { |
73 | 107 | if msg, ok := genericErr["message"].(string); ok { |
74 | 108 | e.Message = msg |
75 | 109 | } |
76 | 110 | if detail, ok := genericErr["detail"].(string); ok { |
77 | 111 | e.Detail = detail |
78 | 112 | } |
79 | | - // Add more fields as needed |
| 113 | + // Optionally add more fields if necessary |
80 | 114 | } |
0 commit comments