Skip to content

Commit

Permalink
don't return copied errors from Copy
Browse files Browse the repository at this point in the history
We _copy_ the error to the response, we only need to return errors encountered
when copying.

fixes #148
  • Loading branch information
Stebalien committed Mar 1, 2019
1 parent 8d99209 commit d345582
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
7 changes: 1 addition & 6 deletions responseemitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,7 @@ func Copy(re ResponseEmitter, res Response) error {
return re.Close()
}

closeErr := re.CloseWithError(err)
if closeErr != nil {
log.Errorf("error closing emitter with error %q: %s", err, closeErr)
}

return err
return re.CloseWithError(err)
}

err = re.Emit(v)
Expand Down
46 changes: 46 additions & 0 deletions responseemitter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmds

import (
"context"
"fmt"
"io"
"testing"

Expand Down Expand Up @@ -51,6 +52,51 @@ func TestCopy(t *testing.T) {
}
}

func TestCopyError(t *testing.T) {
req, err := NewRequest(context.Background(), nil, nil, nil, nil, &Command{})
if err != nil {
t.Fatal(err)
}

fooErr := fmt.Errorf("foo")

re1, res1 := NewChanResponsePair(req)
re2, res2 := NewChanResponsePair(req)

go func() {
err := Copy(re2, res1)
if err != nil {
t.Fatal(err)
}
}()
go func() {
err := re1.Emit("test")
if err != nil {
t.Fatal(err)
}

err = re1.CloseWithError(fooErr)
if err != nil {
t.Fatal(err)
}
}()

v, err := res2.Next()
if err != nil {
t.Fatal(err)
}

str := v.(string)
if str != "test" {
t.Fatalf("expected string %#v but got %#v", "test", str)
}

_, err = res2.Next()
if err != fooErr {
t.Fatalf("expected fooErr but got err=%v", err)
}
}

func TestError(t *testing.T) {
req, err := NewRequest(context.Background(), nil, nil, nil, nil, &Command{})
if err != nil {
Expand Down

0 comments on commit d345582

Please sign in to comment.