fix: resolve openai compat lint issues
This commit is contained in:
parent
2660734a94
commit
84a143361b
2 changed files with 65 additions and 66 deletions
|
|
@ -188,14 +188,18 @@ func (p *Provider) Chat(
|
||||||
|
|
||||||
// Non-200: read a prefix to tell HTML error page apart from JSON error body.
|
// Non-200: read a prefix to tell HTML error page apart from JSON error body.
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
body, err := io.ReadAll(io.LimitReader(resp.Body, 256))
|
body, readErr := io.ReadAll(io.LimitReader(resp.Body, 256))
|
||||||
if err != nil {
|
if readErr != nil {
|
||||||
return nil, fmt.Errorf("failed to read response: %w", err)
|
return nil, fmt.Errorf("failed to read response: %w", readErr)
|
||||||
}
|
}
|
||||||
if looksLikeHTML(body, contentType) {
|
if looksLikeHTML(body, contentType) {
|
||||||
return nil, wrapHTMLResponseError(resp.StatusCode, body, contentType, p.apiBase)
|
return nil, wrapHTMLResponseError(resp.StatusCode, body, contentType, p.apiBase)
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("API request failed:\n Status: %d\n Body: %s", resp.StatusCode, responsePreview(body, 128))
|
return nil, fmt.Errorf(
|
||||||
|
"API request failed:\n Status: %d\n Body: %s",
|
||||||
|
resp.StatusCode,
|
||||||
|
responsePreview(body, 128),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Peek without consuming so the full stream reaches the JSON decoder.
|
// Peek without consuming so the full stream reaches the JSON decoder.
|
||||||
|
|
@ -218,7 +222,13 @@ func (p *Provider) Chat(
|
||||||
|
|
||||||
func wrapHTMLResponseError(statusCode int, body []byte, contentType, apiBase string) error {
|
func wrapHTMLResponseError(statusCode int, body []byte, contentType, apiBase string) error {
|
||||||
respPreview := responsePreview(body, 128)
|
respPreview := responsePreview(body, 128)
|
||||||
return fmt.Errorf("API request failed: %s returned HTML instead of JSON (content-type: %s); check api_base or proxy configuration.\n Status: %d\n Body: %s", apiBase, contentType, statusCode, respPreview)
|
return fmt.Errorf(
|
||||||
|
"API request failed: %s returned HTML instead of JSON (content-type: %s); check api_base or proxy configuration.\n Status: %d\n Body: %s",
|
||||||
|
apiBase,
|
||||||
|
contentType,
|
||||||
|
statusCode,
|
||||||
|
respPreview,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func looksLikeHTML(body []byte, contentType string) bool {
|
func looksLikeHTML(body []byte, contentType string) bool {
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package openai_compat
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
|
@ -235,11 +236,39 @@ func TestProviderChat_JSONHTTPErrorDoesNotReportHTML(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProviderChat_HTMLSuccessResponseReturnsHelpfulError(t *testing.T) {
|
func TestProviderChat_HTMLResponsesReturnHelpfulError(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
contentType string
|
||||||
|
statusCode int
|
||||||
|
body string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "html success response",
|
||||||
|
contentType: "text/html; charset=utf-8",
|
||||||
|
statusCode: http.StatusOK,
|
||||||
|
body: "<!DOCTYPE html><html><body>gateway login</body></html>",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "html error response",
|
||||||
|
contentType: "text/html; charset=utf-8",
|
||||||
|
statusCode: http.StatusBadGateway,
|
||||||
|
body: "<!DOCTYPE html><html><body>bad gateway</body></html>",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "mislabeled html success response",
|
||||||
|
contentType: "application/json",
|
||||||
|
statusCode: http.StatusOK,
|
||||||
|
body: " \r\n\t<!DOCTYPE html><html><body>gateway login</body></html>",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
w.Header().Set("Content-Type", tt.contentType)
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(tt.statusCode)
|
||||||
_, _ = w.Write([]byte("<!DOCTYPE html><html><body>gateway login</body></html>"))
|
_, _ = w.Write([]byte(tt.body))
|
||||||
}))
|
}))
|
||||||
defer server.Close()
|
defer server.Close()
|
||||||
|
|
||||||
|
|
@ -248,28 +277,7 @@ func TestProviderChat_HTMLSuccessResponseReturnsHelpfulError(t *testing.T) {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("expected error, got nil")
|
t.Fatal("expected error, got nil")
|
||||||
}
|
}
|
||||||
if !strings.Contains(err.Error(), "returned HTML instead of JSON") {
|
if !strings.Contains(err.Error(), fmt.Sprintf("Status: %d", tt.statusCode)) {
|
||||||
t.Fatalf("expected helpful HTML error, got %v", err)
|
|
||||||
}
|
|
||||||
if !strings.Contains(err.Error(), "check api_base or proxy configuration") {
|
|
||||||
t.Fatalf("expected configuration hint, got %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestProviderChat_HTMLErrorResponseReturnsHelpfulError(t *testing.T) {
|
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
||||||
w.WriteHeader(http.StatusBadGateway)
|
|
||||||
_, _ = w.Write([]byte("<!DOCTYPE html><html><body>bad gateway</body></html>"))
|
|
||||||
}))
|
|
||||||
defer server.Close()
|
|
||||||
|
|
||||||
p := NewProvider("key", server.URL, "")
|
|
||||||
_, err := p.Chat(t.Context(), []Message{{Role: "user", Content: "hi"}}, nil, "gpt-4o", nil)
|
|
||||||
if err == nil {
|
|
||||||
t.Fatal("expected error, got nil")
|
|
||||||
}
|
|
||||||
if !strings.Contains(err.Error(), "Status: 502") {
|
|
||||||
t.Fatalf("expected status code in error, got %v", err)
|
t.Fatalf("expected status code in error, got %v", err)
|
||||||
}
|
}
|
||||||
if !strings.Contains(err.Error(), "returned HTML instead of JSON") {
|
if !strings.Contains(err.Error(), "returned HTML instead of JSON") {
|
||||||
|
|
@ -278,26 +286,7 @@ func TestProviderChat_HTMLErrorResponseReturnsHelpfulError(t *testing.T) {
|
||||||
if !strings.Contains(err.Error(), "check api_base or proxy configuration") {
|
if !strings.Contains(err.Error(), "check api_base or proxy configuration") {
|
||||||
t.Fatalf("expected configuration hint, got %v", err)
|
t.Fatalf("expected configuration hint, got %v", err)
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
|
|
||||||
func TestProviderChat_MislabeledHTMLSuccessResponseReturnsHelpfulError(t *testing.T) {
|
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
_, _ = w.Write([]byte(" \r\n\t<!DOCTYPE html><html><body>gateway login</body></html>"))
|
|
||||||
}))
|
|
||||||
defer server.Close()
|
|
||||||
|
|
||||||
p := NewProvider("key", server.URL, "")
|
|
||||||
_, err := p.Chat(t.Context(), []Message{{Role: "user", Content: "hi"}}, nil, "gpt-4o", nil)
|
|
||||||
if err == nil {
|
|
||||||
t.Fatal("expected error, got nil")
|
|
||||||
}
|
|
||||||
if !strings.Contains(err.Error(), "returned HTML instead of JSON") {
|
|
||||||
t.Fatalf("expected helpful HTML error, got %v", err)
|
|
||||||
}
|
|
||||||
if !strings.Contains(err.Error(), "check api_base or proxy configuration") {
|
|
||||||
t.Fatalf("expected configuration hint, got %v", err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue