Skip to content

Commit 7db7418

Browse files
committed
Add error handling for non-success status codes in JamfPro API handler
1 parent e37bcb7 commit 7db7418

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

internal/apihandlers/jamfpro/jamfpro_api_handler.go

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import (
4949

5050
_ "embed"
5151

52+
"github.com/deploymenttheory/go-api-http-client/internal/errors"
5253
"github.com/deploymenttheory/go-api-http-client/internal/logger"
5354
"go.uber.org/zap"
5455
)
@@ -277,29 +278,37 @@ func (u *JamfAPIHandler) UnmarshalResponse(resp *http.Response, out interface{},
277278
if strings.Contains(contentType, "text/html") {
278279
errMsg := ExtractErrorMessageFromHTML(string(bodyBytes))
279280
log.Warn("Received HTML content", zap.String("error_message", errMsg), zap.Int("status_code", resp.StatusCode))
280-
return &APIError{
281+
return &errors.APIError{
281282
StatusCode: resp.StatusCode,
282283
Message: errMsg,
283284
}
284285
}
285286

286287
// Check for non-success status codes before attempting to unmarshal
287-
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
288-
// Parse the error details from the response body for JSON content type
289-
if strings.Contains(contentType, "application/json") {
288+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
289+
// Parse the error details from the response body for JSON content type
290+
if strings.Contains(contentType, "application/json") {
290291
description, err := ParseJSONErrorResponse(bodyBytes)
291292
if err != nil {
292-
log.Error("Failed to parse JSON error response", "error", err)
293-
return fmt.Errorf("received non-success status code: %d and failed to parse error response", resp.StatusCode)
293+
// Log the error using the structured logger and return the error
294+
return nil, c.Logger.Error("Failed to parse JSON error response",
295+
zap.Error(err),
296+
zap.Int("status_code", resp.StatusCode),
297+
)
294298
}
295-
return fmt.Errorf("received non-success status code: %d, error: %s", resp.StatusCode, description)
296-
}
297-
298-
// If the response is not JSON or another error occurs, return a generic error message
299-
log.Error("Received non-success status code", "status_code", resp.StatusCode)
300-
return fmt.Errorf("received non-success status code: %d", resp.StatusCode)
299+
// Log the error with description using the structured logger and return the error
300+
return nil, c.Logger.Error("Received non-success status code with JSON response",
301+
zap.Int("status_code", resp.StatusCode),
302+
zap.String("error_description", description),
303+
)
301304
}
302305

306+
// If the response is not JSON or another error occurs, log a generic error message and return an error
307+
return nil, c.Logger.Error("Received non-success status code without JSON response",
308+
zap.Int("status_code", resp.StatusCode),
309+
)
310+
}
311+
303312
// Determine whether the content type is JSON or XML and unmarshal accordingly
304313
switch {
305314
case strings.Contains(contentType, "application/json"):
@@ -312,19 +321,23 @@ func (u *JamfAPIHandler) UnmarshalResponse(resp *http.Response, out interface{},
312321
}
313322

314323
// Handle any errors that occurred during unmarshaling
315-
if err != nil {
316-
// If unmarshalling fails, check if the content might be HTML
317-
if strings.Contains(string(bodyBytes), "<html>") {
324+
if err != nil {
325+
// If unmarshalling fails, check if the content might be HTML
326+
if strings.Contains(string(bodyBytes), "<html>") {
318327
errMsg := ExtractErrorMessageFromHTML(string(bodyBytes))
319-
log.Warn("Received HTML content instead of expected format", "error_message", errMsg, "status_code", resp.StatusCode)
320-
return fmt.Errorf(errMsg)
321-
}
322-
323-
// Log the error and return it
324-
log.Error("Failed to unmarshal response", "error", err)
325-
return fmt.Errorf("failed to unmarshal response: %v", err)
328+
329+
// Log the warning and return an error using the structured logger
330+
return nil, log.Warn("Received HTML content instead of expected format",
331+
zap.String("error_message", errMsg),
332+
zap.Int("status_code", resp.StatusCode),
333+
)
326334
}
327335

336+
// Log the error using the structured logger and return the error
337+
return nil, log.Error("Failed to unmarshal response",
338+
zap.Error(err),
339+
)
340+
328341
return nil
329342
}
330343

internal/httpclient/http_client_bearer_token_auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func (c *Client) RefreshToken(log logger.Logger) error {
140140

141141
if resp.StatusCode != http.StatusOK {
142142
log.Warn("Token refresh response status is not OK", zap.Int("StatusCode", resp.StatusCode))
143-
return errors.HandleAPIError(resp)
143+
return errors.HandleAPIError(resp, log)
144144
}
145145

146146
tokenResp := &TokenResponse{}

0 commit comments

Comments
 (0)