Skip to content

Commit 74a7c86

Browse files
committed
Add error handling for request body unmarshalling
1 parent 2e623c7 commit 74a7c86

File tree

2 files changed

+1
-27
lines changed

2 files changed

+1
-27
lines changed

embeddings.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,7 @@ func (c *Client) CreateEmbeddings(
268268

269269
// Deserialize JSON to map[string]any
270270
var body map[string]any
271-
err = json.Unmarshal(jsonData, &body)
272-
if err != nil {
271+
if err = json.Unmarshal(jsonData, &body); err != nil {
273272
return
274273
}
275274

embeddings_test.go

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@ import (
1515
"github.com/sashabaranov/go-openai/internal/test/checks"
1616
)
1717

18-
// badMarshaler produces invalid JSON when marshaled.
19-
type badMarshaler struct{}
20-
21-
func (badMarshaler) MarshalJSON() ([]byte, error) {
22-
return []byte("{"), nil
23-
}
24-
2518
func TestEmbedding(t *testing.T) {
2619
embeddedModels := []openai.EmbeddingModel{
2720
openai.AdaSimilarity,
@@ -332,21 +325,3 @@ func TestDotProduct(t *testing.T) {
332325
t.Errorf("Expected Vector Length Mismatch Error, but got: %v", err)
333326
}
334327
}
335-
336-
// TestCreateEmbeddings_UnmarshalError verifies that an error is returned when
337-
// the JSON bytes produced by marshaling the request cannot be unmarshaled back
338-
// into a map.
339-
func TestCreateEmbeddings_UnmarshalError(t *testing.T) {
340-
client, server, teardown := setupOpenAITestServer()
341-
defer teardown()
342-
343-
server.RegisterHandler("/v1/embeddings", func(w http.ResponseWriter, _ *http.Request) {
344-
w.WriteHeader(http.StatusOK)
345-
fmt.Fprint(w, `{"data":[]}`)
346-
})
347-
348-
_, err := client.CreateEmbeddings(context.Background(), openai.EmbeddingRequest{
349-
Input: badMarshaler{},
350-
})
351-
checks.HasError(t, err, "CreateEmbeddings should fail on unmarshal error")
352-
}

0 commit comments

Comments
 (0)