Skip to content

Commit aa0b9e0

Browse files
authored
Merge pull request #136 from deploymenttheory/dev
Fix bug in logging fuctionality
2 parents 08bc4b1 + 20961aa commit aa0b9e0

File tree

1 file changed

+40
-67
lines changed

1 file changed

+40
-67
lines changed

response/error.go

Lines changed: 40 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ import (
1717

1818
// APIError represents an api error response.
1919
type APIError struct {
20-
StatusCode int `json:"status_code"` // HTTP status code
21-
Type string `json:"type"` // Type of error
22-
Message string `json:"message"` // Human-readable message
23-
Detail string `json:"detail,omitempty"` // Detailed error message
24-
Errors map[string]interface{} `json:"errors,omitempty"` // Additional error details
25-
Raw string `json:"raw"` // Raw response body for debugging
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+
Message string `json:"message"` // Summary of the error
24+
Details []string `json:"details"` // Detailed error messages, if any
25+
RawResponse string `json:"raw_response"` // Raw response body for debugging
2626
}
2727

2828
// Error returns a string representation of the APIError, making it compatible with the error interface.
@@ -39,21 +39,22 @@ func (e *APIError) Error() string {
3939
}
4040

4141
// Fallback to a simpler error message format if JSON marshaling fails.
42-
return fmt.Sprintf("API Error: StatusCode=%d, Type=%s, Message=%s", e.StatusCode, e.Type, e.Message)
42+
return fmt.Sprintf("API Error: StatusCode=%d, Message=%s", e.StatusCode, e.Message)
4343
}
4444

4545
// HandleAPIErrorResponse handles the HTTP error response from an API and logs the error.
4646
func HandleAPIErrorResponse(resp *http.Response, log logger.Logger) *APIError {
4747
apiError := &APIError{
4848
StatusCode: resp.StatusCode,
49-
Type: "API Error Response",
50-
Message: "An error occurred",
49+
Method: resp.Request.Method,
50+
URL: resp.Request.URL.String(),
51+
Message: "API Error Response",
5152
}
5253

5354
bodyBytes, err := io.ReadAll(resp.Body)
5455
if err != nil {
55-
apiError.Raw = "Failed to read response body"
56-
log.LogError("error_reading_response_body", resp.Request.Method, resp.Request.URL.String(), apiError.StatusCode, resp.Status, err, apiError.Raw)
56+
apiError.RawResponse = "Failed to read response body"
57+
log.LogError("error_reading_response_body", resp.Request.Method, resp.Request.URL.String(), apiError.StatusCode, resp.Status, err, apiError.RawResponse)
5758
return apiError
5859
}
5960

@@ -68,9 +69,9 @@ func HandleAPIErrorResponse(resp *http.Response, log logger.Logger) *APIError {
6869
case "text/plain":
6970
parseTextResponse(bodyBytes, apiError, log, resp)
7071
default:
71-
apiError.Raw = string(bodyBytes)
72+
apiError.RawResponse = string(bodyBytes)
7273
apiError.Message = "Unknown content type error"
73-
log.LogError("unknown_content_type_error", resp.Request.Method, resp.Request.URL.String(), apiError.StatusCode, "Unknown content type", nil, apiError.Raw)
74+
log.LogError("unknown_content_type_error", resp.Request.Method, resp.Request.URL.String(), apiError.StatusCode, "Unknown content type", nil, apiError.RawResponse)
7475
}
7576

7677
return apiError
@@ -93,28 +94,26 @@ func ParseContentTypeHeader(header string) (string, map[string]string) {
9394
// parseJSONResponse attempts to parse the JSON error response and update the APIError structure.
9495
func parseJSONResponse(bodyBytes []byte, apiError *APIError, log logger.Logger, resp *http.Response) {
9596
if err := json.Unmarshal(bodyBytes, apiError); err != nil {
96-
apiError.Raw = string(bodyBytes)
97-
logError(log, apiError, "json_parsing_error", resp.Request.Method, resp.Request.URL.String(), resp.Status, err)
97+
apiError.RawResponse = string(bodyBytes)
98+
log.LogError("json_parsing_error", resp.Request.Method, resp.Request.URL.String(), apiError.StatusCode, resp.Status, err, apiError.RawResponse)
9899
} else {
99100
if apiError.Message == "" {
100101
apiError.Message = "An unknown error occurred"
101102
}
102103

103-
// Log the detected JSON error with all the context information.
104-
logError(log, apiError, "json_error_detected", resp.Request.Method, resp.Request.URL.String(), resp.Status, nil)
104+
log.LogError("json_error_detected", resp.Request.Method, resp.Request.URL.String(), apiError.StatusCode, resp.Status, err, apiError.RawResponse)
105105
}
106106
}
107107

108108
// parseXMLResponse dynamically parses XML error responses and accumulates potential error messages.
109109
func parseXMLResponse(bodyBytes []byte, apiError *APIError, log logger.Logger, resp *http.Response) {
110110
// Always set the Raw field to the entire XML content for debugging purposes.
111-
apiError.Raw = string(bodyBytes)
111+
apiError.RawResponse = string(bodyBytes)
112112

113113
// Parse the XML document.
114114
doc, err := xmlquery.Parse(bytes.NewReader(bodyBytes))
115115
if err != nil {
116-
// Log the XML parsing error with all the context information.
117-
logError(log, apiError, "xml_parsing_error", resp.Request.Method, resp.Request.URL.String(), resp.Status, err)
116+
log.LogError("xml_parsing_error", resp.Request.Method, resp.Request.URL.String(), apiError.StatusCode, resp.Status, err, apiError.RawResponse)
118117
return
119118
}
120119

@@ -141,37 +140,41 @@ func parseXMLResponse(bodyBytes []byte, apiError *APIError, log logger.Logger, r
141140
// Determine the error to log based on whether a message was found.
142141
var logErr error
143142
if apiError.Message == "" {
144-
logErr = fmt.Errorf("No error message extracted from XML")
143+
logErr = fmt.Errorf("no error message extracted from XML")
145144
}
146-
147145
// Log the error or the lack of extracted messages using the centralized logger.
148-
logError(log, apiError, "xml_error_detected", resp.Request.Method, resp.Request.URL.String(), resp.Status, logErr)
146+
log.LogError("html_parsing_error", resp.Request.Method, resp.Request.URL.String(), apiError.StatusCode, resp.Status, logErr, apiError.RawResponse)
149147
}
150148

151149
// parseTextResponse updates the APIError structure based on a plain text error response and logs it.
152150
func parseTextResponse(bodyBytes []byte, apiError *APIError, log logger.Logger, resp *http.Response) {
151+
// Convert the body bytes to a string and assign it to both the message and RawResponse fields of APIError.
153152
bodyText := string(bodyBytes)
154-
apiError.Raw = bodyText
155-
156-
// Check if the 'Message' field of APIError is empty and use the body text as the message.
157-
if apiError.Message == "" {
158-
apiError.Message = bodyText
159-
}
160-
161-
// Use the updated logError function with the additional parameters.
162-
logError(log, apiError, "text_error_detected", resp.Request.Method, resp.Request.URL.String(), resp.Status, nil)
153+
apiError.RawResponse = bodyText
154+
155+
// Directly use the body text as the error message if the Message field is empty.
156+
apiError.Message = bodyText
157+
158+
log.LogError(
159+
"text_error_detected", // Event
160+
resp.Request.Method, // HTTP method
161+
resp.Request.URL.String(), // Request URL
162+
apiError.StatusCode, // HTTP status code
163+
resp.Status, // HTTP status message from the response
164+
nil, // Error (nil because text parsing is unlikely to fail)
165+
apiError.RawResponse, // Raw response text
166+
)
163167
}
164168

165169
// parseHTMLResponse extracts meaningful information from an HTML error response and concatenates all text within <p> tags.
166170
func parseHTMLResponse(bodyBytes []byte, apiError *APIError, log logger.Logger, resp *http.Response) {
167171
// Always set the Raw field to the entire HTML content for debugging purposes.
168-
apiError.Raw = string(bodyBytes)
172+
apiError.RawResponse = string(bodyBytes)
169173

170174
reader := bytes.NewReader(bodyBytes)
171175
doc, err := html.Parse(reader)
172176
if err != nil {
173-
// Log HTML parsing error using centralized logger with context.
174-
logError(log, apiError, "html_parsing_error", resp.Request.Method, resp.Request.URL.String(), resp.Status, err)
177+
log.LogError("html_parsing_error", resp.Request.Method, resp.Request.URL.String(), apiError.StatusCode, resp.Status, err, apiError.RawResponse)
175178
return
176179
}
177180

@@ -212,40 +215,10 @@ func parseHTMLResponse(bodyBytes []byte, apiError *APIError, log logger.Logger,
212215
// Determine the error to log based on whether a message was found.
213216
var logErr error
214217
if apiError.Message == "" {
215-
logErr = fmt.Errorf("No error message extracted from HTML")
218+
logErr = fmt.Errorf("no error message extracted from HTML")
216219
}
217220

218221
// Log the extracted error message or the fallback message using the centralized logger.
219-
logError(log, apiError, "html_error_detected", resp.Request.Method, resp.Request.URL.String(), resp.Status, logErr)
220-
}
221-
222-
// logError logs the error details using the provided logger instance.
223-
// func logError(log logger.Logger, apiError *APIError, event string, resp *http.Response) {
224-
// // Prepare the error message. If apiError.Message is empty, use a default message.
225-
// errorMessage := apiError.Message
226-
// if errorMessage == "" {
227-
// errorMessage = "An unspecified error occurred"
228-
// }
229-
230-
// // Use LogError method from the logger package for error logging.
231-
// log.LogError(
232-
// event,
233-
// resp.Request.Method,
234-
// resp.Request.URL.String(),
235-
// apiError.StatusCode,
236-
// resp.Status,
237-
// fmt.Errorf(errorMessage),
238-
// apiError.Raw,
239-
// )
240-
// }
241-
242-
func logError(log logger.Logger, apiError *APIError, event, method, url, statusMessage string, err error) {
243-
// Prepare the error message. If apiError.Message is empty, use a default message.
244-
errorMessage := apiError.Message
245-
if errorMessage == "" {
246-
errorMessage = "An unspecified error occurred"
247-
}
222+
log.LogError("html_error_detected", resp.Request.Method, resp.Request.URL.String(), apiError.StatusCode, resp.Status, logErr, apiError.RawResponse)
248223

249-
// Call the LogError method from the logger package for error logging.
250-
log.LogError(event, method, url, apiError.StatusCode, statusMessage, err, apiError.Raw)
251224
}

0 commit comments

Comments
 (0)