|
1 | | -// msgraph_api_response.go |
| 1 | +// jamfpro_api_handler.go |
2 | 2 | package msgraph |
3 | 3 |
|
4 | 4 | import ( |
@@ -41,37 +41,6 @@ func (g *GraphAPIHandler) HandleAPISuccessResponse(resp *http.Response, out inte |
41 | 41 | return g.unmarshalResponse(contentType, bodyBytes, out) |
42 | 42 | } |
43 | 43 |
|
44 | | -func (g *GraphAPIHandler) HandleAPIErrorResponse(resp *http.Response, out interface{}, log logger.Logger) error { |
45 | | - // Read the response body |
46 | | - bodyBytes, err := g.readResponseBody(resp) |
47 | | - if err != nil { |
48 | | - return err |
49 | | - } |
50 | | - |
51 | | - // Convert bodyBytes to a string to represent the raw response body |
52 | | - rawResponse := string(bodyBytes) |
53 | | - |
54 | | - // Log the raw response details for debugging |
55 | | - g.logResponseDetails(resp, bodyBytes) |
56 | | - |
57 | | - // Get the content type from the response headers |
58 | | - contentType := resp.Header.Get("Content-Type") |
59 | | - |
60 | | - // Handle known error content types (e.g., JSON, HTML) |
61 | | - if strings.Contains(contentType, "application/json") { |
62 | | - return g.handleErrorJSONResponse(bodyBytes, resp.StatusCode, rawResponse) |
63 | | - } else if strings.Contains(contentType, "text/html") { |
64 | | - return g.handleErrorHTMLResponse(bodyBytes, resp.StatusCode) |
65 | | - } |
66 | | - |
67 | | - // Generic error handling for unknown content types |
68 | | - g.Logger.Error("Received non-success status code without detailed error response", |
69 | | - zap.Int("status_code", resp.StatusCode), |
70 | | - zap.String("raw_response", rawResponse), |
71 | | - ) |
72 | | - return fmt.Errorf("received non-success status code: %d, raw response: %s", resp.StatusCode, rawResponse) |
73 | | -} |
74 | | - |
75 | 44 | // handleDeleteRequest handles the special case for DELETE requests, where a successful response might not contain a body. |
76 | 45 | func (g *GraphAPIHandler) handleDeleteRequest(resp *http.Response) error { |
77 | 46 | if resp.StatusCode >= 200 && resp.StatusCode < 300 { |
@@ -124,41 +93,6 @@ func (g *GraphAPIHandler) handleBinaryData(contentType, contentDisposition strin |
124 | 93 | return nil // If not binary data, no action needed |
125 | 94 | } |
126 | 95 |
|
127 | | -// handleErrorHTMLResponse handles error responses with HTML content by extracting and logging the error message. |
128 | | -func (g *GraphAPIHandler) handleErrorHTMLResponse(bodyBytes []byte, statusCode int) error { |
129 | | - // Extract the error message from the HTML content |
130 | | - htmlErrorMessage := ExtractErrorMessageFromHTML(string(bodyBytes)) |
131 | | - // Log the error message along with the status code |
132 | | - g.Logger.Error("Received HTML error content", |
133 | | - zap.String("error_message", htmlErrorMessage), |
134 | | - zap.Int("status_code", statusCode), |
135 | | - ) |
136 | | - // Return an error with the extracted message |
137 | | - return fmt.Errorf("received HTML error content: %s", htmlErrorMessage) |
138 | | -} |
139 | | - |
140 | | -// handleErrorJSONResponse handles error responses with JSON content by parsing the error message and logging it. |
141 | | -func (g *GraphAPIHandler) handleErrorJSONResponse(bodyBytes []byte, statusCode int, rawResponse string) error { |
142 | | - // Parse the JSON error response to extract the error description |
143 | | - description, err := ParseJSONErrorResponse(bodyBytes) |
144 | | - if err != nil { |
145 | | - // Log the parsing error |
146 | | - g.Logger.Error("Failed to parse JSON error response", |
147 | | - zap.Error(err), |
148 | | - zap.Int("status_code", statusCode), |
149 | | - zap.String("raw_response", rawResponse), // Include raw response in the log |
150 | | - ) |
151 | | - return fmt.Errorf("failed to parse JSON error response: %v, raw response: %s", err, rawResponse) |
152 | | - } |
153 | | - // Log the error description along with the status code and raw response |
154 | | - g.Logger.Error("Received non-success status code with JSON response", |
155 | | - zap.Int("status_code", statusCode), |
156 | | - zap.String("error_description", description), |
157 | | - zap.String("raw_response", rawResponse), // Include raw response in the log |
158 | | - ) |
159 | | - return fmt.Errorf("received non-success status code with JSON response: %s, raw response: %s", description, rawResponse) |
160 | | -} |
161 | | - |
162 | 96 | // unmarshalResponse unmarshals the response body into the provided output structure based on the content type (JSON or XML). |
163 | 97 | func (g *GraphAPIHandler) unmarshalResponse(contentType string, bodyBytes []byte, out interface{}) error { |
164 | 98 | // Determine the content type and unmarshal accordingly |
|
0 commit comments