@@ -17,12 +17,12 @@ import (
17
17
18
18
// APIError represents an api error response.
19
19
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
26
26
}
27
27
28
28
// Error returns a string representation of the APIError, making it compatible with the error interface.
@@ -39,21 +39,22 @@ func (e *APIError) Error() string {
39
39
}
40
40
41
41
// 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 )
43
43
}
44
44
45
45
// HandleAPIErrorResponse handles the HTTP error response from an API and logs the error.
46
46
func HandleAPIErrorResponse (resp * http.Response , log logger.Logger ) * APIError {
47
47
apiError := & APIError {
48
48
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" ,
51
52
}
52
53
53
54
bodyBytes , err := io .ReadAll (resp .Body )
54
55
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 )
57
58
return apiError
58
59
}
59
60
@@ -68,9 +69,9 @@ func HandleAPIErrorResponse(resp *http.Response, log logger.Logger) *APIError {
68
69
case "text/plain" :
69
70
parseTextResponse (bodyBytes , apiError , log , resp )
70
71
default :
71
- apiError .Raw = string (bodyBytes )
72
+ apiError .RawResponse = string (bodyBytes )
72
73
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 )
74
75
}
75
76
76
77
return apiError
@@ -93,28 +94,26 @@ func ParseContentTypeHeader(header string) (string, map[string]string) {
93
94
// parseJSONResponse attempts to parse the JSON error response and update the APIError structure.
94
95
func parseJSONResponse (bodyBytes []byte , apiError * APIError , log logger.Logger , resp * http.Response ) {
95
96
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 )
98
99
} else {
99
100
if apiError .Message == "" {
100
101
apiError .Message = "An unknown error occurred"
101
102
}
102
103
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 )
105
105
}
106
106
}
107
107
108
108
// parseXMLResponse dynamically parses XML error responses and accumulates potential error messages.
109
109
func parseXMLResponse (bodyBytes []byte , apiError * APIError , log logger.Logger , resp * http.Response ) {
110
110
// Always set the Raw field to the entire XML content for debugging purposes.
111
- apiError .Raw = string (bodyBytes )
111
+ apiError .RawResponse = string (bodyBytes )
112
112
113
113
// Parse the XML document.
114
114
doc , err := xmlquery .Parse (bytes .NewReader (bodyBytes ))
115
115
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 )
118
117
return
119
118
}
120
119
@@ -141,37 +140,41 @@ func parseXMLResponse(bodyBytes []byte, apiError *APIError, log logger.Logger, r
141
140
// Determine the error to log based on whether a message was found.
142
141
var logErr error
143
142
if apiError .Message == "" {
144
- logErr = fmt .Errorf ("No error message extracted from XML" )
143
+ logErr = fmt .Errorf ("no error message extracted from XML" )
145
144
}
146
-
147
145
// 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 )
149
147
}
150
148
151
149
// parseTextResponse updates the APIError structure based on a plain text error response and logs it.
152
150
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.
153
152
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
+ )
163
167
}
164
168
165
169
// parseHTMLResponse extracts meaningful information from an HTML error response and concatenates all text within <p> tags.
166
170
func parseHTMLResponse (bodyBytes []byte , apiError * APIError , log logger.Logger , resp * http.Response ) {
167
171
// Always set the Raw field to the entire HTML content for debugging purposes.
168
- apiError .Raw = string (bodyBytes )
172
+ apiError .RawResponse = string (bodyBytes )
169
173
170
174
reader := bytes .NewReader (bodyBytes )
171
175
doc , err := html .Parse (reader )
172
176
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 )
175
178
return
176
179
}
177
180
@@ -212,40 +215,10 @@ func parseHTMLResponse(bodyBytes []byte, apiError *APIError, log logger.Logger,
212
215
// Determine the error to log based on whether a message was found.
213
216
var logErr error
214
217
if apiError .Message == "" {
215
- logErr = fmt .Errorf ("No error message extracted from HTML" )
218
+ logErr = fmt .Errorf ("no error message extracted from HTML" )
216
219
}
217
220
218
221
// 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 )
248
223
249
- // Call the LogError method from the logger package for error logging.
250
- log .LogError (event , method , url , apiError .StatusCode , statusMessage , err , apiError .Raw )
251
224
}
0 commit comments