@@ -41,37 +41,6 @@ func (j *JamfAPIHandler) HandleAPISuccessResponse(resp *http.Response, out inter
41
41
return j .unmarshalResponse (contentType , bodyBytes , out )
42
42
}
43
43
44
- func (j * JamfAPIHandler ) HandleAPIErrorResponse (resp * http.Response , out interface {}, log logger.Logger ) error {
45
- // Read the response body
46
- bodyBytes , err := j .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
- j .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 j .handleErrorJSONResponse (bodyBytes , resp .StatusCode , rawResponse )
63
- } else if strings .Contains (contentType , "text/html" ) {
64
- return j .handleErrorHTMLResponse (bodyBytes , resp .StatusCode )
65
- }
66
-
67
- // Generic error handling for unknown content types
68
- j .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 (j * JamfAPIHandler ) handleDeleteRequest (resp * http.Response ) error {
77
46
if resp .StatusCode >= 200 && resp .StatusCode < 300 {
@@ -124,40 +93,71 @@ func (j *JamfAPIHandler) handleBinaryData(contentType, contentDisposition string
124
93
return nil // If not binary data, no action needed
125
94
}
126
95
96
+ // func (j *JamfAPIHandler) HandleAPIErrorResponse(resp *http.Response, out interface{}, log logger.Logger) error {
97
+ // // Read the response body
98
+ // bodyBytes, err := j.readResponseBody(resp)
99
+ // if err != nil {
100
+ // return err
101
+ // }
102
+
103
+ // // Convert bodyBytes to a string to represent the raw response body
104
+ // rawResponse := string(bodyBytes)
105
+
106
+ // // Log the raw response details for debugging
107
+ // j.logResponseDetails(resp, bodyBytes)
108
+
109
+ // // Get the content type from the response headers
110
+ // contentType := resp.Header.Get("Content-Type")
111
+
112
+ // // Handle known error content types (e.g., JSON, HTML)
113
+ // if strings.Contains(contentType, "application/json") {
114
+ // return j.handleErrorJSONResponse(bodyBytes, resp.StatusCode, rawResponse)
115
+ // } else if strings.Contains(contentType, "text/html") {
116
+ // return j.handleErrorHTMLResponse(bodyBytes, resp.StatusCode)
117
+ // }
118
+
119
+ // // Generic error handling for unknown content types
120
+ // j.Logger.Error("Received non-success status code without detailed error response",
121
+ // zap.Int("status_code", resp.StatusCode),
122
+ // zap.String("raw_response", rawResponse),
123
+ // )
124
+ // return fmt.Errorf("received non-success status code: %d, raw response: %s", resp.StatusCode, rawResponse)
125
+ // }
126
+
127
127
// handleErrorHTMLResponse handles error responses with HTML content by extracting and logging the error message.
128
- func (j * JamfAPIHandler ) 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
- j .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
- }
128
+ // func (j *JamfAPIHandler) 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
+ // j.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
139
140
140
// handleErrorJSONResponse handles error responses with JSON content by parsing the error message and logging it.
141
- func (j * JamfAPIHandler ) 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
- j .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
- j .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
- }
141
+ // func (j *JamfAPIHandler) 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
+ // j.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
+ // j.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
161
162
162
// unmarshalResponse unmarshals the response body into the provided output structure based on the content type (JSON or XML).
163
163
func (j * JamfAPIHandler ) unmarshalResponse (contentType string , bodyBytes []byte , out interface {}) error {
0 commit comments