Skip to content

Commit b706b68

Browse files
committed
fix: add additional fields to APIError struct for Jamf Pro JSON error responses
1 parent a425cc8 commit b706b68

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

response/error.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,24 @@ import (
1717

1818
// APIError represents an api error response.
1919
type APIError struct {
20-
StatusCode int `json:"status_code"` // HTTP status code
21-
Method string `json:"method"` // HTTP method used for the request
22-
URL string `json:"url"` // The URL of the HTTP request
20+
StatusCode int `json:"status_code"` // HTTP status code
21+
Method string `json:"method"` // HTTP method used for the request
22+
URL string `json:"url"` // The URL of the HTTP request
23+
HTTPStatus int `json:"httpStatus,omitempty"`
24+
Errors []Errors `json:"errors,omitempty"`
2325
Message string `json:"message"` // Summary of the error
2426
Details []string `json:"details,omitempty"` // Detailed error messages, if any
2527
RawResponse string `json:"raw_response"` // Raw response body for debugging
2628
}
2729

30+
// Errors represents individual error details within an API error response.
31+
type Errors struct {
32+
Code string `json:"code,omitempty"`
33+
Field string `json:"field,omitempty"`
34+
Description string `json:"description,omitempty"`
35+
ID *string `json:"id,omitempty"`
36+
}
37+
2838
// Error returns a string representation of the APIError, making it compatible with the error interface.
2939
func (e *APIError) Error() string {
3040
data, err := json.Marshal(e)
@@ -74,15 +84,14 @@ func HandleAPIErrorResponse(resp *http.Response, sugar *zap.SugaredLogger) *APIE
7484

7585
// parseJSONResponse attempts to parse the JSON error response and update the APIError structure.
7686
func parseJSONResponse(bodyBytes []byte, apiError *APIError) {
77-
apiError.RawResponse = string(bodyBytes)
78-
7987
if err := json.Unmarshal(bodyBytes, apiError); err != nil {
88+
apiError.RawResponse = string(bodyBytes)
8089
} else {
8190
if apiError.Message == "" {
8291
apiError.Message = "An unknown error occurred"
8392
}
84-
}
8593

94+
}
8695
}
8796

8897
// parseXMLResponse dynamically parses XML error responses and accumulates potential error messages.

0 commit comments

Comments
 (0)