Skip to content

Commit 58af150

Browse files
apoorvajagtapAlexVulaj
authored andcommitted
return errors instead of printing to logs
1 parent e5f1a0a commit 58af150

File tree

4 files changed

+19
-48
lines changed

4 files changed

+19
-48
lines changed

client.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"errors"
1212
"fmt"
1313
"io"
14-
"log"
1514

1615
"net"
1716
"net/http"
@@ -294,10 +293,7 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
294293
}
295294
err = c.SetDeadline(deadline)
296295
if err != nil {
297-
if err := c.Close(); err != nil {
298-
log.Printf("websocket: failed to close network connection: %v", err)
299-
}
300-
return nil, err
296+
return nil, errors.Join(err, c.Close())
301297
}
302298
return c, nil
303299
}
@@ -336,9 +332,7 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
336332

337333
defer func() {
338334
if netConn != nil {
339-
if err := netConn.Close(); err != nil {
340-
log.Printf("websocket: failed to close network connection: %v", err)
341-
}
335+
netConn.Close() //#nosec:G104 (CWE-703)
342336
}
343337
}()
344338

@@ -433,7 +427,7 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
433427
return nil, nil, err
434428
}
435429
netConn = nil // to avoid close in defer.
436-
return conn, resp, nil
430+
return conn, resp, err
437431
}
438432

439433
func cloneTLSConfig(cfg *tls.Config) *tls.Config {

client_server_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -430,10 +430,10 @@ func TestDialBadOrigin(t *testing.T) {
430430
ws.Close()
431431
t.Fatalf("Dial: nil")
432432
}
433-
if resp == nil {
433+
if resp == nil { // nolint:staticcheck
434434
t.Fatalf("resp=nil, err=%v", err)
435435
}
436-
if resp.StatusCode != http.StatusForbidden {
436+
if resp.StatusCode != http.StatusForbidden { // nolint:staticcheck
437437
t.Fatalf("status=%d, want %d", resp.StatusCode, http.StatusForbidden)
438438
}
439439
}
@@ -551,11 +551,11 @@ func TestRespOnBadHandshake(t *testing.T) {
551551
t.Fatalf("Dial: nil")
552552
}
553553

554-
if resp == nil {
554+
if resp == nil { // nolint:staticcheck
555555
t.Fatalf("resp=nil, err=%v", err)
556556
}
557557

558-
if resp.StatusCode != expectedStatus {
558+
if resp.StatusCode != expectedStatus { // nolint:staticcheck
559559
t.Errorf("resp.StatusCode=%d, want %d", resp.StatusCode, expectedStatus)
560560
}
561561

proxy.go

+3-13
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"bufio"
99
"encoding/base64"
1010
"errors"
11-
"log"
1211
"net"
1312
"net/http"
1413
"net/url"
@@ -58,29 +57,20 @@ func (hpd *httpProxyDialer) Dial(network string, addr string) (net.Conn, error)
5857
}
5958

6059
if err := connectReq.Write(conn); err != nil {
61-
if err := conn.Close(); err != nil {
62-
log.Printf("httpProxyDialer: failed to close connection: %v", err)
63-
}
64-
return nil, err
60+
return nil, errors.Join(err, conn.Close())
6561
}
6662

6763
// Read response. It's OK to use and discard buffered reader here becaue
6864
// the remote server does not speak until spoken to.
6965
br := bufio.NewReader(conn)
7066
resp, err := http.ReadResponse(br, connectReq)
7167
if err != nil {
72-
if err := conn.Close(); err != nil {
73-
log.Printf("httpProxyDialer: failed to close connection: %v", err)
74-
}
75-
return nil, err
68+
return nil, errors.Join(err, conn.Close())
7669
}
7770

7871
if resp.StatusCode != http.StatusOK {
79-
if err := conn.Close(); err != nil {
80-
log.Printf("httpProxyDialer: failed to close connection: %v", err)
81-
}
8272
f := strings.SplitN(resp.Status, " ", 2)
83-
return nil, errors.New(f[1])
73+
return nil, errors.Join(errors.New(f[1]), conn.Close())
8474
}
8575
return conn, nil
8676
}

server.go

+9-22
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"bufio"
99
"errors"
1010
"io"
11-
"log"
1211
"net/http"
1312
"net/url"
1413
"strings"
@@ -180,10 +179,10 @@ func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeade
180179
}
181180

182181
if brw.Reader.Buffered() > 0 {
183-
if err := netConn.Close(); err != nil {
184-
log.Printf("websocket: failed to close network connection: %v", err)
185-
}
186-
return nil, errors.New("websocket: client sent data before handshake is complete")
182+
return nil, errors.Join(
183+
errors.New("websocket: client sent data before handshake is complete"),
184+
netConn.Close(),
185+
)
187186
}
188187

189188
var br *bufio.Reader
@@ -248,32 +247,20 @@ func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeade
248247

249248
// Clear deadlines set by HTTP server.
250249
if err := netConn.SetDeadline(time.Time{}); err != nil {
251-
if err := netConn.Close(); err != nil {
252-
log.Printf("websocket: failed to close network connection: %v", err)
253-
}
254-
return nil, err
250+
return nil, errors.Join(err, netConn.Close())
255251
}
256252

257253
if u.HandshakeTimeout > 0 {
258254
if err := netConn.SetWriteDeadline(time.Now().Add(u.HandshakeTimeout)); err != nil {
259-
if err := netConn.Close(); err != nil {
260-
log.Printf("websocket: failed to close network connection: %v", err)
261-
}
262-
return nil, err
255+
return nil, errors.Join(err, netConn.Close())
263256
}
264257
}
265258
if _, err = netConn.Write(p); err != nil {
266-
if err := netConn.Close(); err != nil {
267-
log.Printf("websocket: failed to close network connection: %v", err)
268-
}
269-
return nil, err
259+
return nil, errors.Join(err, netConn.Close())
270260
}
271261
if u.HandshakeTimeout > 0 {
272262
if err := netConn.SetWriteDeadline(time.Time{}); err != nil {
273-
if err := netConn.Close(); err != nil {
274-
log.Printf("websocket: failed to close network connection: %v", err)
275-
}
276-
return nil, err
263+
return nil, errors.Join(err, netConn.Close())
277264
}
278265
}
279266

@@ -376,7 +363,7 @@ func bufioWriterBuffer(originalWriter io.Writer, bw *bufio.Writer) []byte {
376363
panic(err)
377364
}
378365
if err := bw.Flush(); err != nil {
379-
log.Printf("websocket: bufioWriterBuffer: Flush: %v", err)
366+
panic(err)
380367
}
381368

382369
bw.Reset(originalWriter)

0 commit comments

Comments
 (0)