Skip to content

Commit 9f22056

Browse files
committed
Refactor HTML error response parsing
1 parent 257d0cc commit 9f22056

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

response/error.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,31 +157,30 @@ func parseTextResponse(bodyBytes []byte, apiError *APIError, log logger.Logger,
157157

158158
// parseHTMLResponse extracts meaningful information from an HTML error response.
159159
func parseHTMLResponse(bodyBytes []byte, apiError *APIError, log logger.Logger, resp *http.Response) {
160-
// Convert the response body to a reader for the HTML parser
160+
// Always set the Raw field to the entire HTML content for debugging purposes
161+
apiError.Raw = string(bodyBytes)
162+
161163
reader := bytes.NewReader(bodyBytes)
162164
doc, err := html.Parse(reader)
163165
if err != nil {
164-
apiError.Raw = string(bodyBytes)
165166
logError(log, apiError, "html_parsing_error", resp)
166167
return
167168
}
168169

169170
var parse func(*html.Node)
170171
parse = func(n *html.Node) {
171-
// Look for <p> tags that might contain error messages
172172
if n.Type == html.ElementNode && n.Data == "p" {
173173
if n.FirstChild != nil {
174-
// Assuming the error message is in the text content of a <p> tag
175174
apiError.Message = n.FirstChild.Data
176-
return // Stop after finding the first <p> tag with content
175+
// Optionally, you might break or return after finding the first relevant message
177176
}
178177
}
179178
for c := n.FirstChild; c != nil; c = c.NextSibling {
180-
parse(c) // Recursively search for <p> tags in child nodes
179+
parse(c)
181180
}
182181
}
183182

184-
parse(doc) // Start parsing from the document node
183+
parse(doc)
185184

186185
// If no <p> tag was found or it was empty, fallback to using the raw HTML
187186
if apiError.Message == "" {

0 commit comments

Comments
 (0)