Skip to content

Commit 53cba73

Browse files
committed
fix: resolve openai compat lint issues
1 parent 6eaa49f commit 53cba73

File tree

2 files changed

+64
-65
lines changed

2 files changed

+64
-65
lines changed

pkg/providers/openai_compat/provider.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,18 @@ func (p *Provider) Chat(
188188

189189
// Non-200: read a prefix to tell HTML error page apart from JSON error body.
190190
if resp.StatusCode != http.StatusOK {
191-
body, err := io.ReadAll(io.LimitReader(resp.Body, 256))
192-
if err != nil {
193-
return nil, fmt.Errorf("failed to read response: %w", err)
191+
body, readErr := io.ReadAll(io.LimitReader(resp.Body, 256))
192+
if readErr != nil {
193+
return nil, fmt.Errorf("failed to read response: %w", readErr)
194194
}
195195
if looksLikeHTML(body, contentType) {
196196
return nil, wrapHTMLResponseError(resp.StatusCode, body, contentType, p.apiBase)
197197
}
198-
return nil, fmt.Errorf("API request failed:\n Status: %d\n Body: %s", resp.StatusCode, responsePreview(body, 128))
198+
return nil, fmt.Errorf(
199+
"API request failed:\n Status: %d\n Body: %s",
200+
resp.StatusCode,
201+
responsePreview(body, 128),
202+
)
199203
}
200204

201205
// Peek without consuming so the full stream reaches the JSON decoder.
@@ -218,7 +222,13 @@ func (p *Provider) Chat(
218222

219223
func wrapHTMLResponseError(statusCode int, body []byte, contentType, apiBase string) error {
220224
respPreview := responsePreview(body, 128)
221-
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)
225+
return fmt.Errorf(
226+
"API request failed: %s returned HTML instead of JSON (content-type: %s); check api_base or proxy configuration.\n Status: %d\n Body: %s",
227+
apiBase,
228+
contentType,
229+
statusCode,
230+
respPreview,
231+
)
222232
}
223233

224234
func looksLikeHTML(body []byte, contentType string) bool {

pkg/providers/openai_compat/provider_test.go

Lines changed: 49 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package openai_compat
33
import (
44
"bytes"
55
"encoding/json"
6+
"fmt"
67
"io"
78
"net/http"
89
"net/http/httptest"
@@ -235,69 +236,57 @@ func TestProviderChat_JSONHTTPErrorDoesNotReportHTML(t *testing.T) {
235236
}
236237
}
237238

238-
func TestProviderChat_HTMLSuccessResponseReturnsHelpfulError(t *testing.T) {
239-
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
240-
w.Header().Set("Content-Type", "text/html; charset=utf-8")
241-
w.WriteHeader(http.StatusOK)
242-
_, _ = w.Write([]byte("<!DOCTYPE html><html><body>gateway login</body></html>"))
243-
}))
244-
defer server.Close()
245-
246-
p := NewProvider("key", server.URL, "")
247-
_, err := p.Chat(t.Context(), []Message{{Role: "user", Content: "hi"}}, nil, "gpt-4o", nil)
248-
if err == nil {
249-
t.Fatal("expected error, got nil")
250-
}
251-
if !strings.Contains(err.Error(), "returned HTML instead of JSON") {
252-
t.Fatalf("expected helpful HTML error, got %v", err)
253-
}
254-
if !strings.Contains(err.Error(), "check api_base or proxy configuration") {
255-
t.Fatalf("expected configuration hint, got %v", err)
256-
}
257-
}
258-
259-
func TestProviderChat_HTMLErrorResponseReturnsHelpfulError(t *testing.T) {
260-
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
261-
w.Header().Set("Content-Type", "text/html; charset=utf-8")
262-
w.WriteHeader(http.StatusBadGateway)
263-
_, _ = w.Write([]byte("<!DOCTYPE html><html><body>bad gateway</body></html>"))
264-
}))
265-
defer server.Close()
266-
267-
p := NewProvider("key", server.URL, "")
268-
_, err := p.Chat(t.Context(), []Message{{Role: "user", Content: "hi"}}, nil, "gpt-4o", nil)
269-
if err == nil {
270-
t.Fatal("expected error, got nil")
271-
}
272-
if !strings.Contains(err.Error(), "Status: 502") {
273-
t.Fatalf("expected status code in error, got %v", err)
274-
}
275-
if !strings.Contains(err.Error(), "returned HTML instead of JSON") {
276-
t.Fatalf("expected helpful HTML error, got %v", err)
277-
}
278-
if !strings.Contains(err.Error(), "check api_base or proxy configuration") {
279-
t.Fatalf("expected configuration hint, got %v", err)
239+
func TestProviderChat_HTMLResponsesReturnHelpfulError(t *testing.T) {
240+
tests := []struct {
241+
name string
242+
contentType string
243+
statusCode int
244+
body string
245+
}{
246+
{
247+
name: "html success response",
248+
contentType: "text/html; charset=utf-8",
249+
statusCode: http.StatusOK,
250+
body: "<!DOCTYPE html><html><body>gateway login</body></html>",
251+
},
252+
{
253+
name: "html error response",
254+
contentType: "text/html; charset=utf-8",
255+
statusCode: http.StatusBadGateway,
256+
body: "<!DOCTYPE html><html><body>bad gateway</body></html>",
257+
},
258+
{
259+
name: "mislabeled html success response",
260+
contentType: "application/json",
261+
statusCode: http.StatusOK,
262+
body: " \r\n\t<!DOCTYPE html><html><body>gateway login</body></html>",
263+
},
280264
}
281-
}
282265

283-
func TestProviderChat_MislabeledHTMLSuccessResponseReturnsHelpfulError(t *testing.T) {
284-
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
285-
w.Header().Set("Content-Type", "application/json")
286-
w.WriteHeader(http.StatusOK)
287-
_, _ = w.Write([]byte(" \r\n\t<!DOCTYPE html><html><body>gateway login</body></html>"))
288-
}))
289-
defer server.Close()
266+
for _, tt := range tests {
267+
t.Run(tt.name, func(t *testing.T) {
268+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
269+
w.Header().Set("Content-Type", tt.contentType)
270+
w.WriteHeader(tt.statusCode)
271+
_, _ = w.Write([]byte(tt.body))
272+
}))
273+
defer server.Close()
290274

291-
p := NewProvider("key", server.URL, "")
292-
_, err := p.Chat(t.Context(), []Message{{Role: "user", Content: "hi"}}, nil, "gpt-4o", nil)
293-
if err == nil {
294-
t.Fatal("expected error, got nil")
295-
}
296-
if !strings.Contains(err.Error(), "returned HTML instead of JSON") {
297-
t.Fatalf("expected helpful HTML error, got %v", err)
298-
}
299-
if !strings.Contains(err.Error(), "check api_base or proxy configuration") {
300-
t.Fatalf("expected configuration hint, got %v", err)
275+
p := NewProvider("key", server.URL, "")
276+
_, err := p.Chat(t.Context(), []Message{{Role: "user", Content: "hi"}}, nil, "gpt-4o", nil)
277+
if err == nil {
278+
t.Fatal("expected error, got nil")
279+
}
280+
if !strings.Contains(err.Error(), fmt.Sprintf("Status: %d", tt.statusCode)) {
281+
t.Fatalf("expected status code in error, got %v", err)
282+
}
283+
if !strings.Contains(err.Error(), "returned HTML instead of JSON") {
284+
t.Fatalf("expected helpful HTML error, got %v", err)
285+
}
286+
if !strings.Contains(err.Error(), "check api_base or proxy configuration") {
287+
t.Fatalf("expected configuration hint, got %v", err)
288+
}
289+
})
301290
}
302291
}
303292

0 commit comments

Comments
 (0)