Skip to content

Commit

Permalink
return errors instead of printing to logs
Browse files Browse the repository at this point in the history
  • Loading branch information
apoorvajagtap committed Feb 14, 2024
1 parent d293aa5 commit b535127
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 48 deletions.
12 changes: 3 additions & 9 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"errors"
"fmt"
"io"
"log"

"net"
"net/http"
Expand Down Expand Up @@ -294,10 +293,7 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
}
err = c.SetDeadline(deadline)
if err != nil {
if err := c.Close(); err != nil {
log.Printf("websocket: failed to close network connection: %v", err)
}
return nil, err
return nil, errors.Join(err, c.Close())

Check warning on line 296 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L296

Added line #L296 was not covered by tests
}
return c, nil
}
Expand Down Expand Up @@ -336,9 +332,7 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h

defer func() {
if netConn != nil {
if err := netConn.Close(); err != nil {
log.Printf("websocket: failed to close network connection: %v", err)
}
err = netConn.Close()
}
}()

Expand Down Expand Up @@ -433,7 +427,7 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
return nil, nil, err
}
netConn = nil // to avoid close in defer.
return conn, resp, nil
return conn, resp, err
}

func cloneTLSConfig(cfg *tls.Config) *tls.Config {
Expand Down
8 changes: 4 additions & 4 deletions client_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,10 +430,10 @@ func TestDialBadOrigin(t *testing.T) {
ws.Close()
t.Fatalf("Dial: nil")
}
if resp == nil {
if resp == nil { // nolint:staticcheck
t.Fatalf("resp=nil, err=%v", err)
}
if resp.StatusCode != http.StatusForbidden {
if resp.StatusCode != http.StatusForbidden { // nolint:staticcheck
t.Fatalf("status=%d, want %d", resp.StatusCode, http.StatusForbidden)
}
}
Expand Down Expand Up @@ -551,11 +551,11 @@ func TestRespOnBadHandshake(t *testing.T) {
t.Fatalf("Dial: nil")
}

if resp == nil {
if resp == nil { // nolint:staticcheck
t.Fatalf("resp=nil, err=%v", err)
}

if resp.StatusCode != expectedStatus {
if resp.StatusCode != expectedStatus { // nolint:staticcheck
t.Errorf("resp.StatusCode=%d, want %d", resp.StatusCode, expectedStatus)
}

Expand Down
16 changes: 3 additions & 13 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"bufio"
"encoding/base64"
"errors"
"log"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -58,29 +57,20 @@ func (hpd *httpProxyDialer) Dial(network string, addr string) (net.Conn, error)
}

if err := connectReq.Write(conn); err != nil {
if err := conn.Close(); err != nil {
log.Printf("httpProxyDialer: failed to close connection: %v", err)
}
return nil, err
return nil, errors.Join(conn.Close(), err)

Check warning on line 60 in proxy.go

View check run for this annotation

Codecov / codecov/patch

proxy.go#L60

Added line #L60 was not covered by tests
}

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

Check warning on line 68 in proxy.go

View check run for this annotation

Codecov / codecov/patch

proxy.go#L68

Added line #L68 was not covered by tests
}

if resp.StatusCode != http.StatusOK {
if err := conn.Close(); err != nil {
log.Printf("httpProxyDialer: failed to close connection: %v", err)
}
f := strings.SplitN(resp.Status, " ", 2)
return nil, errors.New(f[1])
return nil, errors.Join(conn.Close(), err, errors.New(f[1]))

Check warning on line 73 in proxy.go

View check run for this annotation

Codecov / codecov/patch

proxy.go#L73

Added line #L73 was not covered by tests
}
return conn, nil
}
31 changes: 9 additions & 22 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"bufio"
"errors"
"io"
"log"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -180,10 +179,10 @@ func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeade
}

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

Check warning on line 185 in server.go

View check run for this annotation

Codecov / codecov/patch

server.go#L182-L185

Added lines #L182 - L185 were not covered by tests
}

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

// Clear deadlines set by HTTP server.
if err := netConn.SetDeadline(time.Time{}); err != nil {
if err := netConn.Close(); err != nil {
log.Printf("websocket: failed to close network connection: %v", err)
}
return nil, err
return nil, errors.Join(netConn.Close(), err)

Check warning on line 250 in server.go

View check run for this annotation

Codecov / codecov/patch

server.go#L250

Added line #L250 was not covered by tests
}

if u.HandshakeTimeout > 0 {
if err := netConn.SetWriteDeadline(time.Now().Add(u.HandshakeTimeout)); err != nil {
if err := netConn.Close(); err != nil {
log.Printf("websocket: failed to close network connection: %v", err)
}
return nil, err
return nil, errors.Join(netConn.Close(), err)

Check warning on line 255 in server.go

View check run for this annotation

Codecov / codecov/patch

server.go#L255

Added line #L255 was not covered by tests
}
}
if _, err = netConn.Write(p); err != nil {
if err := netConn.Close(); err != nil {
log.Printf("websocket: failed to close network connection: %v", err)
}
return nil, err
return nil, errors.Join(netConn.Close(), err)

Check warning on line 259 in server.go

View check run for this annotation

Codecov / codecov/patch

server.go#L259

Added line #L259 was not covered by tests
}
if u.HandshakeTimeout > 0 {
if err := netConn.SetWriteDeadline(time.Time{}); err != nil {
if err := netConn.Close(); err != nil {
log.Printf("websocket: failed to close network connection: %v", err)
}
return nil, err
return nil, errors.Join(netConn.Close(), err)

Check warning on line 263 in server.go

View check run for this annotation

Codecov / codecov/patch

server.go#L263

Added line #L263 was not covered by tests
}
}

Expand Down Expand Up @@ -376,7 +363,7 @@ func bufioWriterBuffer(originalWriter io.Writer, bw *bufio.Writer) []byte {
panic(err)
}
if err := bw.Flush(); err != nil {
log.Printf("websocket: bufioWriterBuffer: Flush: %v", err)
panic(err)

Check warning on line 366 in server.go

View check run for this annotation

Codecov / codecov/patch

server.go#L366

Added line #L366 was not covered by tests
}

bw.Reset(originalWriter)
Expand Down

0 comments on commit b535127

Please sign in to comment.