Skip to content

Commit b2165d8

Browse files
nishubhartiNishu Bharti
andauthored
fix unhandled error from ingester (#61)
* fix unhandled error from ingester Signed-off-by: Nishu Bharti <nishu.bhart1@ibm.com> * minor update Signed-off-by: Nishu Bharti <nishu.bhart1@ibm.com> * add unit test Signed-off-by: Nishu Bharti <nishu.bhart1@ibm.com> * minor fix Signed-off-by: Nishu Bharti <nishu.bhart1@ibm.com> --------- Signed-off-by: Nishu Bharti <nishu.bhart1@ibm.com> Co-authored-by: Nishu Bharti <nishu.bhart1@ibm.com>
1 parent 93a0cd8 commit b2165d8

File tree

2 files changed

+54
-26
lines changed

2 files changed

+54
-26
lines changed

fluent/client/ws_client.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ import (
2828
"bytes"
2929
"crypto/tls"
3030
"errors"
31+
"fmt"
32+
"io"
3133
"net/http"
34+
"strconv"
3235
"sync"
3336

3437
"github.com/IBM/fluent-forward-go/fluent/client/ws"
@@ -112,7 +115,14 @@ func (wcf *DefaultWSConnectionFactory) New() (ext.Conn, error) {
112115

113116
conn, resp, err := dialer.Dial(wcf.URL, header)
114117
if resp != nil && resp.Body != nil {
115-
// TODO: dump response, which is second return value from Dial
118+
bodyBytes, readErr := io.ReadAll(resp.Body)
119+
if readErr == nil {
120+
bodyString := string(bodyBytes)
121+
if resp.StatusCode >= 300 {
122+
err = fmt.Errorf("%s. %s:%s", err, strconv.Itoa(resp.StatusCode), bodyString)
123+
}
124+
}
125+
116126
resp.Body.Close()
117127
}
118128

fluent/client/ws_client_test.go

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ var _ = Describe("IAMAuthInfo", func() {
6161

6262
var _ = Describe("DefaultWSConnectionFactory", func() {
6363
var (
64-
svr *httptest.Server
65-
happy chan struct{}
66-
useTLS bool
64+
svr *httptest.Server
65+
ch chan struct{}
66+
useTLS, testError bool
6767
)
6868

69-
newHandler := func(happy chan struct{}) http.Handler {
69+
happyHandler := func(ch chan struct{}) http.Handler {
7070
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
7171
defer GinkgoRecover()
7272
svrOpts := ws.ConnectionOptions{}
@@ -82,19 +82,27 @@ var _ = Describe("DefaultWSConnectionFactory", func() {
8282
Fail("broke")
8383
}
8484

85-
happy <- struct{}{}
85+
ch <- struct{}{}
8686

8787
svrConnection.Close()
8888
})
8989
}
9090

91+
sadHandler := func(ch chan struct{}) http.Handler {
92+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
93+
http.Error(w, "broken test", http.StatusInternalServerError)
94+
})
95+
}
96+
9197
JustBeforeEach(func() {
92-
happy = make(chan struct{})
98+
ch = make(chan struct{})
9399

94100
if useTLS {
95-
svr = httptest.NewTLSServer(newHandler(happy))
101+
svr = httptest.NewTLSServer(happyHandler(ch))
102+
} else if testError {
103+
svr = httptest.NewTLSServer(sadHandler(ch))
96104
} else {
97-
svr = httptest.NewServer(newHandler(happy))
105+
svr = httptest.NewServer(happyHandler(ch))
98106
}
99107

100108
time.Sleep(5 * time.Millisecond)
@@ -118,10 +126,36 @@ var _ = Describe("DefaultWSConnectionFactory", func() {
118126
})
119127

120128
Expect(cli.Connect()).ToNot(HaveOccurred())
121-
Eventually(happy).Should(Receive())
129+
Eventually(ch).Should(Receive())
122130
Expect(cli.Disconnect()).ToNot(HaveOccurred())
123131
})
124132

133+
When("sends wrong url, expects error", func() {
134+
135+
BeforeEach(func() {
136+
testError = true
137+
})
138+
139+
It("returns error", func() {
140+
u := "ws" + strings.TrimPrefix(svr.URL, "http")
141+
142+
cli := fclient.NewWS(client.WSConnectionOptions{
143+
Factory: &client.DefaultWSConnectionFactory{
144+
URL: u + "/test",
145+
TLSConfig: &tls.Config{
146+
InsecureSkipVerify: true,
147+
},
148+
AuthInfo: NewIAMAuthInfo("oi"),
149+
},
150+
})
151+
152+
err := cli.Connect()
153+
Expect(err).To(HaveOccurred())
154+
Expect(err.Error()).To(ContainSubstring("websocket: bad handshake. 500:broken test"))
155+
})
156+
157+
})
158+
125159
When("the factory is configured for TLS", func() {
126160
BeforeEach(func() {
127161
useTLS = true
@@ -204,22 +238,6 @@ var _ = Describe("WSClient", func() {
204238
})
205239
})
206240

207-
When("the factory returns an error", func() {
208-
var (
209-
connectionError error
210-
)
211-
212-
JustBeforeEach(func() {
213-
connectionError = errors.New("Nope")
214-
factory.NewReturns(nil, connectionError)
215-
})
216-
217-
It("Returns an error", func() {
218-
err := client.Connect()
219-
Expect(err).To(HaveOccurred())
220-
Expect(err).To(BeIdenticalTo(connectionError))
221-
})
222-
})
223241
})
224242

225243
Describe("Disconnect", func() {

0 commit comments

Comments
 (0)