Merge pull request #3168 from Alix-007/fix/model-list-http-error-read

fix(model): handle error response read failures
This commit is contained in:
Mauro 2026-06-25 08:42:36 +02:00 committed by GitHub
commit 0a47fc8eb1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 26 additions and 4 deletions

View file

@ -4,6 +4,7 @@ import (
"bytes"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
@ -68,6 +69,21 @@ func TestFetchOpenAIModels_HTTPError(t *testing.T) {
assert.Contains(t, err.Error(), "HTTP 401")
}
func TestFetchOpenAIModels_HTTPErrorReadFailure(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
body := "short"
w.Header().Set("Content-Length", strconv.Itoa(len(body)+1))
w.WriteHeader(http.StatusUnauthorized)
_, _ = w.Write([]byte(body))
}))
defer srv.Close()
_, err := fetchOpenAIModels(srv.URL, "bad")
require.Error(t, err)
assert.Contains(t, err.Error(), "read error response")
assert.Contains(t, err.Error(), "unexpected EOF")
}
func TestFetchOpenAIModels_EmptyDataEnvelope(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(`{"data":[]}`))

View file

@ -45,7 +45,10 @@ func fetchOpenAIModels(baseURL, apiKey string) ([]modelEntry, error) {
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(io.LimitReader(resp.Body, 512))
body, readErr := io.ReadAll(io.LimitReader(resp.Body, 512))
if readErr != nil {
return nil, fmt.Errorf("read error response: %w", readErr)
}
return nil, fmt.Errorf("HTTP %d: %s", resp.StatusCode, strings.TrimSpace(string(body)))
}

View file

@ -160,9 +160,12 @@ func (p *Provider) buildRequestBody(
// treat it as false — web_search_preview must not be injected
// when the caller cannot express a well-typed intent.
if _, present := options["native_search"]; present {
log.Printf(
"[openai_compat] native_search option has unexpected type %T, ignoring",
options["native_search"],
logger.WarnCF(
"provider.openai_compat",
"native_search option has unexpected type, ignoring",
map[string]any{
"type": fmt.Sprintf("%T", options["native_search"]),
},
)
}
}