fix(model): handle error response read failures
This commit is contained in:
parent
540f81ff9e
commit
5e3fd4cd0f
2 changed files with 20 additions and 1 deletions
|
|
@ -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":[]}`))
|
||||
|
|
|
|||
|
|
@ -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, err := io.ReadAll(io.LimitReader(resp.Body, 512))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read error response: %w", err)
|
||||
}
|
||||
return nil, fmt.Errorf("HTTP %d: %s", resp.StatusCode, strings.TrimSpace(string(body)))
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue