@@ -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
0 commit comments