Skip to content

Commit 1dd8308

Browse files
committed
Handle unmarshaling errors and log HTML error messages
1 parent 2e59ac4 commit 1dd8308

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

apihandlers/jamfpro/jamfpro_api_handler.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -294,16 +294,29 @@ func (u *JamfAPIHandler) UnmarshalResponse(resp *http.Response, out interface{},
294294
}
295295
}
296296
*/
297-
if strings.Contains(contentType, "text/html") {
298-
htmlErrorMessages := ExtractErrorMessageFromHTML(string(bodyBytes))
297+
// Handle any errors that occurred during unmarshaling
298+
if err != nil {
299+
// If unmarshalling fails, check if the content might be HTML
300+
if strings.Contains(string(bodyBytes), "<html>") {
301+
htmlErrorMessages := ExtractErrorMessageFromHTML(string(bodyBytes))
299302

300-
for _, msg := range htmlErrorMessages {
301-
for key, value := range msg {
302-
log.Error("HTML Error", zap.String(key, value))
303+
// Combine all HTML error messages into a single string
304+
var combinedErrMsgs []string
305+
for _, msg := range htmlErrorMessages {
306+
for _, value := range msg {
307+
combinedErrMsgs = append(combinedErrMsgs, value)
308+
}
303309
}
304-
}
310+
combinedErrMsg := strings.Join(combinedErrMsgs, "; ")
311+
312+
// Log the combined error message as a single entry
313+
log.Error("Received HTML content instead of expected format",
314+
zap.String("error_message", combinedErrMsg),
315+
zap.Int("status_code", resp.StatusCode),
316+
)
305317

306-
return fmt.Errorf("HTML error encountered")
318+
return fmt.Errorf("received HTML content instead of expected format: %s", combinedErrMsg)
319+
}
307320
}
308321

309322
// Check for non-success status codes before attempting to unmarshal

0 commit comments

Comments
 (0)