Skip to content

Commit 366dd46

Browse files
committed
fix: address golangci-lint issues
- Replace type assertions with errors.As for errorlint compliance - Break long lines to stay under 120 character limit - Maintain all existing functionality and test coverage
1 parent 3a8bf4f commit 366dd46

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

stream_reader.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"bytes"
66
"encoding/json"
7+
"errors"
78
"fmt"
89
"io"
910
"net/http"
@@ -43,9 +44,11 @@ func (stream *streamReader[T]) Recv() (response T, err error) {
4344
if err != nil {
4445
// If we get a JSON parsing error, it might be because we got an error event
4546
// Check if we have accumulated error data
46-
if _, ok := err.(*json.SyntaxError); ok && len(stream.errAccumulator.Bytes()) > 0 {
47+
var syntaxErr *json.SyntaxError
48+
if errors.As(err, &syntaxErr) && len(stream.errAccumulator.Bytes()) > 0 {
4749
// We have error data, return a more informative error
48-
return response, fmt.Errorf("failed to parse response (error event received): %s", string(stream.errAccumulator.Bytes()))
50+
return response, fmt.Errorf("failed to parse response (error event received): %s",
51+
string(stream.errAccumulator.Bytes()))
4952
}
5053
return
5154
}

stream_reader_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ func TestStreamReaderRecvRaw(t *testing.T) {
8181
func TestStreamReaderParsesErrorEvents(t *testing.T) {
8282
// Test case simulating Groq's error event format
8383
errorEvent := `event: error
84-
data: {"error":{"message":"Invalid tool_call: tool \"name_unknown\" does not exist.","type":"invalid_request_error","code":"invalid_tool_call"}}
84+
data: {"error":{"message":"Invalid tool_call: tool \"name_unknown\" does not exist.",` +
85+
`"type":"invalid_request_error","code":"invalid_tool_call"}}
8586
8687
`
8788
stream := &streamReader[ChatCompletionStreamResponse]{
@@ -98,8 +99,8 @@ data: {"error":{"message":"Invalid tool_call: tool \"name_unknown\" does not exi
9899
}
99100

100101
// Verify it's an APIError
101-
apiErr, ok := err.(*APIError)
102-
if !ok {
102+
var apiErr *APIError
103+
if !errors.As(err, &apiErr) {
103104
t.Fatalf("Expected APIError type but got %T: %v", err, err)
104105
}
105106

@@ -145,8 +146,8 @@ data: [DONE]
145146
}
146147

147148
// Verify it's an APIError
148-
apiErr, ok := err.(*APIError)
149-
if !ok {
149+
var apiErr *APIError
150+
if !errors.As(err, &apiErr) {
150151
t.Fatalf("Expected APIError type but got %T: %v", err, err)
151152
}
152153

@@ -175,8 +176,8 @@ data: {"error":{"message":"Second error","type":"error_type_2"}}
175176
if err1 == nil {
176177
t.Fatal("Expected first error but got nil")
177178
}
178-
apiErr1, ok := err1.(*APIError)
179-
if !ok {
179+
var apiErr1 *APIError
180+
if !errors.As(err1, &apiErr1) {
180181
t.Fatalf("Expected APIError type but got %T: %v", err1, err1)
181182
}
182183
if apiErr1.Message != "First error" {
@@ -188,8 +189,8 @@ data: {"error":{"message":"Second error","type":"error_type_2"}}
188189
if err2 == nil {
189190
t.Fatal("Expected second error but got nil")
190191
}
191-
apiErr2, ok := err2.(*APIError)
192-
if !ok {
192+
var apiErr2 *APIError
193+
if !errors.As(err2, &apiErr2) {
193194
t.Fatalf("Expected APIError type but got %T: %v", err2, err2)
194195
}
195196
if apiErr2.Message != "Second error" {

0 commit comments

Comments
 (0)