@@ -154,7 +154,7 @@ func parseTextResponse(bodyBytes []byte, apiError *APIError, log logger.Logger,
154
154
logError (log , apiError , "text_error_detected" , resp )
155
155
}
156
156
157
- // parseHTMLResponse extracts meaningful information from an HTML error response.
157
+ // parseHTMLResponse extracts meaningful information from an HTML error response and concatenates all text within <p> tags .
158
158
func parseHTMLResponse (bodyBytes []byte , apiError * APIError , log logger.Logger , resp * http.Response ) {
159
159
// Always set the Raw field to the entire HTML content for debugging purposes
160
160
apiError .Raw = string (bodyBytes )
@@ -166,26 +166,32 @@ func parseHTMLResponse(bodyBytes []byte, apiError *APIError, log logger.Logger,
166
166
return
167
167
}
168
168
169
+ var messages []string // To accumulate messages from all <p> tags
169
170
var parse func (* html.Node )
170
171
parse = func (n * html.Node ) {
171
172
if n .Type == html .ElementNode && n .Data == "p" {
172
- if n .FirstChild != nil {
173
- apiError .Message = n .FirstChild .Data
174
- // Optionally, you might break or return after finding the first relevant message
173
+ for c := n .FirstChild ; c != nil ; c = c .NextSibling {
174
+ if c .Type == html .TextNode {
175
+ // Accumulate text content of <p> tag
176
+ messages = append (messages , c .Data )
177
+ }
175
178
}
176
179
}
177
180
for c := n .FirstChild ; c != nil ; c = c .NextSibling {
178
- parse (c )
181
+ parse (c ) // Recursively parse the document
179
182
}
180
183
}
181
184
182
185
parse (doc )
183
186
184
- // If no <p> tag was found or it was empty, fallback to using the raw HTML
185
- if apiError .Message == "" {
187
+ // Concatenate all accumulated messages with a separator
188
+ if len (messages ) > 0 {
189
+ apiError .Message = strings .Join (messages , "; " )
190
+ } else {
191
+ // Fallback error message if no specific messages were extracted
186
192
apiError .Message = "HTML Error: See 'Raw' field for details."
187
- apiError .Raw = string (bodyBytes )
188
193
}
194
+
189
195
// Log the extracted error message or the fallback message
190
196
logError (log , apiError , "html_error_detected" , resp )
191
197
}
0 commit comments