Skip to content

Commit a8eea98

Browse files
committed
Refactor error handling in JamfAPIHandler
1 parent 12dbde0 commit a8eea98

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

apihandlers/jamfpro/jamfpro_api_response.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ func (j *JamfAPIHandler) HandleResponse(resp *http.Response, out interface{}, lo
2828
return err
2929
}
3030

31+
// Convert bodyBytes to a string to represent the raw response body
32+
rawResponse := string(bodyBytes)
33+
3134
// Log the raw response details for debugging
3235
j.logResponseDetails(resp, bodyBytes)
3336

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

127131
// handleErrorJSONResponse handles error responses with JSON content by parsing the error message and logging it.
128-
func (j *JamfAPIHandler) handleErrorJSONResponse(bodyBytes []byte, statusCode int) error {
132+
func (j *JamfAPIHandler) handleErrorJSONResponse(bodyBytes []byte, statusCode int, rawResponse string) error {
129133
// Parse the JSON error response to extract the error description
130134
description, err := ParseJSONErrorResponse(bodyBytes)
131135
if err != nil {
132136
// Log the parsing error
133137
j.Logger.Error("Failed to parse JSON error response",
134138
zap.Error(err),
135139
zap.Int("status_code", statusCode),
140+
zap.String("raw_response", rawResponse), // Include raw response in the log
136141
)
137-
return err
142+
return fmt.Errorf("failed to parse JSON error response: %v, raw response: %s", err, rawResponse)
138143
}
139-
// Log the error description along with the status code
144+
// Log the error description along with the status code and raw response
140145
j.Logger.Error("Received non-success status code with JSON response",
141146
zap.Int("status_code", statusCode),
142147
zap.String("error_description", description),
148+
zap.String("raw_response", rawResponse), // Include raw response in the log
143149
)
144-
// Return an error with the description
145-
return fmt.Errorf("received non-success status code with JSON response: %s", description)
150+
return fmt.Errorf("received non-success status code with JSON response: %s, raw response: %s", description, rawResponse)
146151
}
147152

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

0 commit comments

Comments
 (0)