Skip to content

Commit

Permalink
add the response to openai error if decoding failed #162
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Oct 26, 2024
1 parent 5a09134 commit cbdbf98
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/tgspam/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (o *openAIChecker) sendRequest(msg string) (response openAIResponse, err er
}

if err := json.Unmarshal([]byte(resp.Choices[0].Message.Content), &response); err != nil {
return openAIResponse{}, fmt.Errorf("can't unmarshal response: %w", err)
return openAIResponse{}, fmt.Errorf("can't unmarshal response: %s - %w", resp.Choices[0].Message.Content, err)
}

return response, nil
Expand Down
6 changes: 4 additions & 2 deletions lib/tgspam/openai_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ func TestOpenAIChecker_Check(t *testing.T) {
t.Logf("spam: %v, details: %+v", spam, details)
assert.False(t, spam)
assert.Equal(t, "openai", details.Name)
assert.Equal(t, "OpenAI error: can't unmarshal response: invalid character 'b' looking for beginning of value", details.Details)
assert.Equal(t, "can't unmarshal response: invalid character 'b' looking for beginning of value", details.Error.Error())
assert.Equal(t, "OpenAI error: can't unmarshal response: bad json - invalid character 'b' looking for beginning of value",
details.Details)
assert.Equal(t, "can't unmarshal response: bad json - invalid character 'b' looking for beginning of value",
details.Error.Error())
})

t.Run("no choices", func(t *testing.T) {
Expand Down

0 comments on commit cbdbf98

Please sign in to comment.