generated from deploymenttheory/Template
/
jamfpro_api_error_messages.go
115 lines (99 loc) · 3.2 KB
/
jamfpro_api_error_messages.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// jamfpro_api_error_messages.go
package jamfpro
import (
"bytes"
"encoding/json"
"io"
"net/http"
"strings"
"github.com/PuerkitoBio/goquery"
)
// APIHandlerError represents an error response from the Jamf Pro API.
type APIHandlerError struct {
HTTPStatusCode int `json:"httpStatusCode"`
ErrorType string `json:"errorType"`
ErrorMessage string `json:"errorMessage"`
ExtraDetails map[string]interface{} `json:"extraDetails"`
}
// ReturnAPIErrorResponse parses an HTTP error response from the Jamf Pro API.
func (j *JamfAPIHandler) ReturnAPIErrorResponse(resp *http.Response) *APIHandlerError {
var errorMessage, errorType string
var extraDetails map[string]interface{}
// Safely read the response body
bodyBytes, readErr := io.ReadAll(resp.Body)
if readErr != nil {
return &APIHandlerError{
HTTPStatusCode: resp.StatusCode,
ErrorType: "ReadError",
ErrorMessage: "Failed to read response body",
}
}
// Ensure the body can be re-read for subsequent operations
resp.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
contentType := resp.Header.Get("Content-Type")
// Handle JSON content type
if strings.Contains(contentType, "application/json") {
description, parseErr := ParseJSONErrorResponse(bodyBytes)
if parseErr == nil {
errorMessage = description
errorType = "JSONError"
} else {
errorMessage = "Failed to parse JSON error response: " + parseErr.Error()
}
} else if strings.Contains(contentType, "text/html") {
// Handle HTML content type
bodyBytes, err := io.ReadAll(resp.Body)
if err == nil {
errorMessage = ExtractErrorMessageFromHTML(string(bodyBytes))
errorType = "HTMLError"
} else {
errorMessage = "Failed to read response body for HTML error parsing"
}
} else {
// Fallback for unhandled content types
errorMessage = "An unknown error occurred"
}
return &APIHandlerError{
HTTPStatusCode: resp.StatusCode,
ErrorType: errorType,
ErrorMessage: errorMessage,
ExtraDetails: extraDetails,
}
}
// ExtractErrorMessageFromHTML attempts to parse an HTML error page and extract a combined human-readable error message.
func ExtractErrorMessageFromHTML(htmlContent string) string {
r := bytes.NewReader([]byte(htmlContent))
doc, err := goquery.NewDocumentFromReader(r)
if err != nil {
return "Unable to parse HTML content"
}
var messages []string
doc.Find("p").Each(func(i int, s *goquery.Selection) {
text := strings.TrimSpace(s.Text())
if text != "" {
messages = append(messages, text)
}
})
combinedMessage := strings.Join(messages, " - ")
return combinedMessage
}
// ParseJSONErrorResponse parses the JSON error message from the response body.
func ParseJSONErrorResponse(body []byte) (string, error) {
var errorResponse struct {
HTTPStatus int `json:"httpStatus"`
Errors []struct {
Code string `json:"code"`
Description string `json:"description"`
ID string `json:"id"`
Field string `json:"field"`
} `json:"errors"`
}
err := json.Unmarshal(body, &errorResponse)
if err != nil {
return "", err
}
if len(errorResponse.Errors) > 0 {
return errorResponse.Errors[0].Description, nil
}
return "No error description available", nil
}