From bdaf6204f66a765d7c9d4cfcc08c009afa28d64f Mon Sep 17 00:00:00 2001 From: Matt Low Date: Sun, 5 May 2024 07:32:35 +0000 Subject: [PATCH] Add openai response error handling --- pkg/lmcli/provider/openai/openai.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pkg/lmcli/provider/openai/openai.go b/pkg/lmcli/provider/openai/openai.go index 6dd053f..1a34a17 100644 --- a/pkg/lmcli/provider/openai/openai.go +++ b/pkg/lmcli/provider/openai/openai.go @@ -166,7 +166,14 @@ func (c *OpenAIClient) sendRequest(ctx context.Context, req *http.Request) (*htt req.Header.Set("Authorization", "Bearer "+c.APIKey) client := &http.Client{} - return client.Do(req.WithContext(ctx)) + resp, err := client.Do(req.WithContext(ctx)) + + if resp.StatusCode != 200 { + bytes, _ := io.ReadAll(resp.Body) + return resp, fmt.Errorf("%v", string(bytes)) + } + + return resp, err } func (c *OpenAIClient) CreateChatCompletion(