return errors instead of printing to logs

This commit is contained in:
apoorvajagtap 2024-02-14 23:38:08 +05:30 committed by Alex Vulaj
parent e5f1a0aad0
commit 58af150309
4 changed files with 19 additions and 48 deletions

View File

@ -11,7 +11,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"log"
"net" "net"
"net/http" "net/http"
@ -294,10 +293,7 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
} }
err = c.SetDeadline(deadline) err = c.SetDeadline(deadline)
if err != nil { if err != nil {
if err := c.Close(); err != nil { return nil, errors.Join(err, c.Close())
log.Printf("websocket: failed to close network connection: %v", err)
}
return nil, err
} }
return c, nil return c, nil
} }
@ -336,9 +332,7 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
defer func() { defer func() {
if netConn != nil { if netConn != nil {
if err := netConn.Close(); err != nil { netConn.Close() //#nosec:G104 (CWE-703)
log.Printf("websocket: failed to close network connection: %v", err)
}
} }
}() }()
@ -433,7 +427,7 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
return nil, nil, err return nil, nil, err
} }
netConn = nil // to avoid close in defer. netConn = nil // to avoid close in defer.
return conn, resp, nil return conn, resp, err
} }
func cloneTLSConfig(cfg *tls.Config) *tls.Config { func cloneTLSConfig(cfg *tls.Config) *tls.Config {

View File

@ -430,10 +430,10 @@ func TestDialBadOrigin(t *testing.T) {
ws.Close() ws.Close()
t.Fatalf("Dial: nil") t.Fatalf("Dial: nil")
} }
if resp == nil { if resp == nil { // nolint:staticcheck
t.Fatalf("resp=nil, err=%v", err) 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) t.Fatalf("status=%d, want %d", resp.StatusCode, http.StatusForbidden)
} }
} }
@ -551,11 +551,11 @@ func TestRespOnBadHandshake(t *testing.T) {
t.Fatalf("Dial: nil") t.Fatalf("Dial: nil")
} }
if resp == nil { if resp == nil { // nolint:staticcheck
t.Fatalf("resp=nil, err=%v", err) 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) t.Errorf("resp.StatusCode=%d, want %d", resp.StatusCode, expectedStatus)
} }

View File

@ -8,7 +8,6 @@ import (
"bufio" "bufio"
"encoding/base64" "encoding/base64"
"errors" "errors"
"log"
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
@ -58,10 +57,7 @@ func (hpd *httpProxyDialer) Dial(network string, addr string) (net.Conn, error)
} }
if err := connectReq.Write(conn); err != nil { if err := connectReq.Write(conn); err != nil {
if err := conn.Close(); err != nil { return nil, errors.Join(err, conn.Close())
log.Printf("httpProxyDialer: failed to close connection: %v", err)
}
return nil, err
} }
// Read response. It's OK to use and discard buffered reader here becaue // Read response. It's OK to use and discard buffered reader here becaue
@ -69,18 +65,12 @@ func (hpd *httpProxyDialer) Dial(network string, addr string) (net.Conn, error)
br := bufio.NewReader(conn) br := bufio.NewReader(conn)
resp, err := http.ReadResponse(br, connectReq) resp, err := http.ReadResponse(br, connectReq)
if err != nil { if err != nil {
if err := conn.Close(); err != nil { return nil, errors.Join(err, conn.Close())
log.Printf("httpProxyDialer: failed to close connection: %v", err)
}
return nil, err
} }
if resp.StatusCode != http.StatusOK { 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) f := strings.SplitN(resp.Status, " ", 2)
return nil, errors.New(f[1]) return nil, errors.Join(errors.New(f[1]), conn.Close())
} }
return conn, nil return conn, nil
} }

View File

@ -8,7 +8,6 @@ import (
"bufio" "bufio"
"errors" "errors"
"io" "io"
"log"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
@ -180,10 +179,10 @@ func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeade
} }
if brw.Reader.Buffered() > 0 { if brw.Reader.Buffered() > 0 {
if err := netConn.Close(); err != nil { return nil, errors.Join(
log.Printf("websocket: failed to close network connection: %v", err) errors.New("websocket: client sent data before handshake is complete"),
} netConn.Close(),
return nil, errors.New("websocket: client sent data before handshake is complete") )
} }
var br *bufio.Reader var br *bufio.Reader
@ -248,32 +247,20 @@ func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeade
// Clear deadlines set by HTTP server. // Clear deadlines set by HTTP server.
if err := netConn.SetDeadline(time.Time{}); err != nil { if err := netConn.SetDeadline(time.Time{}); err != nil {
if err := netConn.Close(); err != nil { return nil, errors.Join(err, netConn.Close())
log.Printf("websocket: failed to close network connection: %v", err)
}
return nil, err
} }
if u.HandshakeTimeout > 0 { if u.HandshakeTimeout > 0 {
if err := netConn.SetWriteDeadline(time.Now().Add(u.HandshakeTimeout)); err != nil { if err := netConn.SetWriteDeadline(time.Now().Add(u.HandshakeTimeout)); err != nil {
if err := netConn.Close(); err != nil { return nil, errors.Join(err, netConn.Close())
log.Printf("websocket: failed to close network connection: %v", err)
}
return nil, err
} }
} }
if _, err = netConn.Write(p); err != nil { if _, err = netConn.Write(p); err != nil {
if err := netConn.Close(); err != nil { return nil, errors.Join(err, netConn.Close())
log.Printf("websocket: failed to close network connection: %v", err)
}
return nil, err
} }
if u.HandshakeTimeout > 0 { if u.HandshakeTimeout > 0 {
if err := netConn.SetWriteDeadline(time.Time{}); err != nil { if err := netConn.SetWriteDeadline(time.Time{}); err != nil {
if err := netConn.Close(); err != nil { return nil, errors.Join(err, netConn.Close())
log.Printf("websocket: failed to close network connection: %v", err)
}
return nil, err
} }
} }
@ -376,7 +363,7 @@ func bufioWriterBuffer(originalWriter io.Writer, bw *bufio.Writer) []byte {
panic(err) panic(err)
} }
if err := bw.Flush(); err != nil { if err := bw.Flush(); err != nil {
log.Printf("websocket: bufioWriterBuffer: Flush: %v", err) panic(err)
} }
bw.Reset(originalWriter) bw.Reset(originalWriter)