Skip to content

Commit

Permalink
Refactor error logging in Jamf API handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ShocOne committed Feb 12, 2024
1 parent 8771f75 commit 8df16b2
Showing 1 changed file with 22 additions and 33 deletions.
55 changes: 22 additions & 33 deletions apihandlers/jamfpro/jamfpro_api_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,22 +286,13 @@ func (u *JamfAPIHandler) UnmarshalResponse(resp *http.Response, out interface{},

// Check for non-success status codes
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
// If the content type is HTML, extract and log the error messages
// If the content type is HTML, extract and log the error message
if strings.Contains(contentType, "text/html") {
htmlErrorDetails := ExtractErrorMessageFromHTML(string(bodyBytes))

var combinedErrMsgs []string
for _, detail := range htmlErrorDetails {
for key, value := range detail {
// Combine the key-value pairs into a single string
combinedErrMsgs = append(combinedErrMsgs, key+": "+value)
}
}
combinedErrMsg := strings.Join(combinedErrMsgs, " | ")
htmlErrorMessage := ExtractErrorMessageFromHTML(string(bodyBytes))

// Log the combined HTML error message using Zap
// Log the HTML error message using Zap
log.Error("Received HTML error content",
zap.String("error_message", combinedErrMsg),
zap.String("error_message", htmlErrorMessage),
zap.Int("status_code", resp.StatusCode),
)
} else {
Expand Down Expand Up @@ -355,27 +346,25 @@ func (u *JamfAPIHandler) UnmarshalResponse(resp *http.Response, out interface{},
if err != nil {
// If unmarshalling fails, check if the content might be HTML
if strings.Contains(string(bodyBytes), "<html>") {
htmlErrorMessages := ExtractErrorMessageFromHTML(string(bodyBytes))

for _, msg := range htmlErrorMessages {
for key, value := range msg {
// Log each HTML error detail as a separate field
log.Warn("Received HTML content instead of expected format",
zap.String("HTML Error Detail", fmt.Sprintf("%s: %s", key, value)),
zap.Int("status_code", resp.StatusCode),
)
}
}
htmlErrorMessage := ExtractErrorMessageFromHTML(string(bodyBytes))

// Construct a combined error message for the return error
var combinedErrMsgs []string
for _, msg := range htmlErrorMessages {
for _, value := range msg {
combinedErrMsgs = append(combinedErrMsgs, value)
}
}
combinedErrMsg := strings.Join(combinedErrMsgs, " | ")
return fmt.Errorf("received HTML content instead of expected format: %s", combinedErrMsg)
// Log the HTML error message
log.Warn("Received HTML content instead of expected format",
zap.String("error_message", htmlErrorMessage),
zap.Int("status_code", resp.StatusCode),
)

// Use the HTML error message for logging the error
log.Error("Unmarshal error with HTML content",
zap.String("error_message", htmlErrorMessage),
zap.Int("status_code", resp.StatusCode),
)

Check warning

Code scanning / gosec

Errors unhandled. Warning

Errors unhandled.
} else {
// If the error is not due to HTML content, log the original error
log.Error("Unmarshal error",
zap.Error(err),
zap.Int("status_code", resp.StatusCode),
)

Check warning

Code scanning / gosec

Errors unhandled. Warning

Errors unhandled.
}
}

Expand Down

0 comments on commit 8df16b2

Please sign in to comment.