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:
commit
0a47fc8eb1
3 changed files with 26 additions and 4 deletions
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
|
@ -68,6 +69,21 @@ func TestFetchOpenAIModels_HTTPError(t *testing.T) {
|
||||||
assert.Contains(t, err.Error(), "HTTP 401")
|
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) {
|
func TestFetchOpenAIModels_EmptyDataEnvelope(t *testing.T) {
|
||||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||||
_, _ = w.Write([]byte(`{"data":[]}`))
|
_, _ = w.Write([]byte(`{"data":[]}`))
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,10 @@ func fetchOpenAIModels(baseURL, apiKey string) ([]modelEntry, error) {
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
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)))
|
return nil, fmt.Errorf("HTTP %d: %s", resp.StatusCode, strings.TrimSpace(string(body)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -160,9 +160,12 @@ func (p *Provider) buildRequestBody(
|
||||||
// treat it as false — web_search_preview must not be injected
|
// treat it as false — web_search_preview must not be injected
|
||||||
// when the caller cannot express a well-typed intent.
|
// when the caller cannot express a well-typed intent.
|
||||||
if _, present := options["native_search"]; present {
|
if _, present := options["native_search"]; present {
|
||||||
log.Printf(
|
logger.WarnCF(
|
||||||
"[openai_compat] native_search option has unexpected type %T, ignoring",
|
"provider.openai_compat",
|
||||||
options["native_search"],
|
"native_search option has unexpected type, ignoring",
|
||||||
|
map[string]any{
|
||||||
|
"type": fmt.Sprintf("%T", options["native_search"]),
|
||||||
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue