Skip to content

Refactor error handling in JamfAPIHandler #55

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions apihandlers/jamfpro/jamfpro_api_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ func (j *JamfAPIHandler) HandleResponse(resp *http.Response, out interface{}, lo
return err
}

// Convert bodyBytes to a string to represent the raw response body
rawResponse := string(bodyBytes)

// Log the raw response details for debugging
j.logResponseDetails(resp, bodyBytes)

Expand All @@ -46,11 +49,12 @@ func (j *JamfAPIHandler) HandleResponse(resp *http.Response, out interface{}, lo
if strings.Contains(contentType, "text/html") {
return j.handleErrorHTMLResponse(bodyBytes, resp.StatusCode)
} else if strings.Contains(contentType, "application/json") {
return j.handleErrorJSONResponse(bodyBytes, resp.StatusCode)
return j.handleErrorJSONResponse(bodyBytes, resp.StatusCode, rawResponse)
}
// Log generic error for unknown content types
j.Logger.Error("Received non-success status code without detailed error response",
zap.Int("status_code", resp.StatusCode),
zap.String("raw_response", rawResponse),
)
return fmt.Errorf("received non-success status code: %d", resp.StatusCode)
}
Expand Down Expand Up @@ -125,24 +129,25 @@ func (j *JamfAPIHandler) handleErrorHTMLResponse(bodyBytes []byte, statusCode in
}

// handleErrorJSONResponse handles error responses with JSON content by parsing the error message and logging it.
func (j *JamfAPIHandler) handleErrorJSONResponse(bodyBytes []byte, statusCode int) error {
func (j *JamfAPIHandler) handleErrorJSONResponse(bodyBytes []byte, statusCode int, rawResponse string) error {
// Parse the JSON error response to extract the error description
description, err := ParseJSONErrorResponse(bodyBytes)
if err != nil {
// Log the parsing error
j.Logger.Error("Failed to parse JSON error response",
zap.Error(err),
zap.Int("status_code", statusCode),
zap.String("raw_response", rawResponse), // Include raw response in the log
)
return err
return fmt.Errorf("failed to parse JSON error response: %v, raw response: %s", err, rawResponse)
}
// Log the error description along with the status code
// Log the error description along with the status code and raw response
j.Logger.Error("Received non-success status code with JSON response",
zap.Int("status_code", statusCode),
zap.String("error_description", description),
zap.String("raw_response", rawResponse), // Include raw response in the log
)
// Return an error with the description
return fmt.Errorf("received non-success status code with JSON response: %s", description)
return fmt.Errorf("received non-success status code with JSON response: %s, raw response: %s", description, rawResponse)
}

// unmarshalResponse unmarshals the response body into the provided output structure based on the content type (JSON or XML).
Expand Down