fix(provider): improve error message when API returns HTML instead of JSON

When API endpoint is misconfigured (e.g., wrong api_base URL), the API
returns an HTML error page. Previously, the error message was cryptic:
"failed to unmarshal response: invalid character '<' looking for
beginning of value".

Now we detect HTML responses and provide a clear error message guiding
users to check their api_base URL configuration.

Fixes #1068

🤖 AI Code Generation: Fully AI generated - AI implemented the fix
and I reviewed and tested it
This commit is contained in:
liugangjian 2026-03-04 18:35:32 +08:00
parent 028605cfd0
commit f08fedc4a1

View file

@ -223,9 +223,15 @@ func parseResponse(body []byte) (*LLMResponse, error) {
}
if err := json.Unmarshal(body, &apiResponse); err != nil {
// Check if the response is HTML instead of JSON (e.g., wrong api_base URL)
bodyStr := string(body)
if strings.HasPrefix(strings.TrimSpace(bodyStr), "<") {
return nil, fmt.Errorf("API returned HTML instead of JSON. Please check your api_base URL configuration. The server may be returning an error page or the endpoint URL may be incorrect (e.g., missing '/v1' path)")
}
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
if len(apiResponse.Choices) == 0 {
return &LLMResponse{
Content: "",