Skip to content

Commit

Permalink
fix: handle error in response
Browse files Browse the repository at this point in the history
  • Loading branch information
cyx authored and rhansen2 committed Jun 3, 2022
1 parent 4788faf commit e67bd5f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
19 changes: 14 additions & 5 deletions protocol/describeconfigs/describeconfigs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package describeconfigs

import (
"fmt"
"strconv"

"github.com/segmentio/kafka-go/protocol"
Expand Down Expand Up @@ -87,11 +88,19 @@ func (r *Response) Merge(requests []protocol.Message, results []interface{}) (
response := &Response{}

for _, result := range results {
brokerResp := result.(*Response)
response.Resources = append(
response.Resources,
brokerResp.Resources...,
)
switch v := result.(type) {
case *Response:
response.Resources = append(
response.Resources,
v.Resources...,
)

case error:
return nil, v

default:
return nil, fmt.Errorf("Unknown result type: %T", result)
}
}

return response, nil
Expand Down
57 changes: 57 additions & 0 deletions protocol/describeconfigs/describeconfigs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package describeconfigs

import (
"io"
"reflect"
"testing"

"github.com/segmentio/kafka-go/protocol"
)

func TestResponse_Merge(t *testing.T) {
t.Run("happy path", func(t *testing.T) {
r := &Response{}

r1 := &Response{
Resources: []ResponseResource{
{ResourceName: "r1"},
},
}
r2 := &Response{
Resources: []ResponseResource{
{ResourceName: "r2"},
},
}

got, err := r.Merge([]protocol.Message{&Request{}}, []interface{}{r1, r2})
if err != nil {
t.Fatal(err)
}

want := &Response{
Resources: []ResponseResource{
{ResourceName: "r1"},
{ResourceName: "r2"},
},
}

if !reflect.DeepEqual(want, got) {
t.Fatalf("wanted response: \n%+v, got \n%+v", want, got)
}
})

t.Run("with errors", func(t *testing.T) {
r := &Response{}

r1 := &Response{
Resources: []ResponseResource{
{ResourceName: "r1"},
},
}

_, err := r.Merge([]protocol.Message{&Request{}}, []interface{}{r1, io.EOF})
if err != io.EOF {
t.Fatalf("wanted err io.EOF, got %v", err)
}
})
}

0 comments on commit e67bd5f

Please sign in to comment.