From 72c0b8a564c1dd60bd7997667804d970b41c6b33 Mon Sep 17 00:00:00 2001 From: Anmol Sethi <hi@nhooyr.io> Date: Sat, 30 Mar 2019 23:04:10 -0500 Subject: [PATCH] Fix error wrapping verbs Updates #46 --- accept.go | 4 ++-- datatype.go | 1 + dial.go | 4 ++-- json.go | 8 ++++---- opcode.go | 1 + statuscode.go | 1 + websocket.go | 22 +++++++++++----------- 7 files changed, 22 insertions(+), 19 deletions(-) diff --git a/accept.go b/accept.go index 2d07b93..0236d65 100644 --- a/accept.go +++ b/accept.go @@ -118,7 +118,7 @@ func Accept(w http.ResponseWriter, r *http.Request, opts ...AcceptOption) (*Conn netConn, brw, err := hj.Hijack() if err != nil { - err = xerrors.Errorf("websocket: failed to hijack connection: %v", err) + err = xerrors.Errorf("websocket: failed to hijack connection: %w", err) http.Error(w, err.Error(), http.StatusInternalServerError) return nil, err } @@ -165,7 +165,7 @@ func authenticateOrigin(r *http.Request, origins []string) error { } u, err := url.Parse(origin) if err != nil { - return xerrors.Errorf("failed to parse Origin header %q: %v", origin, err) + return xerrors.Errorf("failed to parse Origin header %q: %w", origin, err) } for _, o := range origins { if u.Host == o { diff --git a/datatype.go b/datatype.go index fff40f4..c2473df 100644 --- a/datatype.go +++ b/datatype.go @@ -2,6 +2,7 @@ package websocket // DataType represents the Opcode of a WebSocket data frame. type DataType int + //go:generate go run golang.org/x/tools/cmd/stringer -type=DataType // DataType constants. diff --git a/dial.go b/dial.go index d64f568..0370f82 100644 --- a/dial.go +++ b/dial.go @@ -74,7 +74,7 @@ func Dial(ctx context.Context, u string, opts ...DialOption) (_ *Conn, _ *http.R parsedURL, err := url.Parse(u) if err != nil { - return nil, nil, xerrors.Errorf("failed to parse websocket url: %v", err) + return nil, nil, xerrors.Errorf("failed to parse websocket url: %w", err) } switch parsedURL.Scheme { @@ -113,7 +113,7 @@ func Dial(ctx context.Context, u string, opts ...DialOption) (_ *Conn, _ *http.R }() if resp.StatusCode != http.StatusSwitchingProtocols { - return nil, resp, xerrors.Errorf("websocket: expected status code %v but got %v", http.StatusSwitchingProtocols) + return nil, resp, xerrors.Errorf("websocket: expected status code %v but got %v", http.StatusSwitchingProtocols, resp.StatusCode) } if !httpguts.HeaderValuesContainsToken(resp.Header["Connection"], "Upgrade") { diff --git a/json.go b/json.go index 3c62c52..97eb7c6 100644 --- a/json.go +++ b/json.go @@ -12,7 +12,7 @@ import ( func ReadJSON(ctx context.Context, c *Conn, v interface{}) error { typ, r, err := c.ReadMessage(ctx) if err != nil { - return xerrors.Errorf("failed to read json: %v", err) + return xerrors.Errorf("failed to read json: %w", err) } if typ != websocket.TextFrame { @@ -25,7 +25,7 @@ func ReadJSON(ctx context.Context, c *Conn, v interface{}) error { d := json.NewDecoder(r) err = d.Decode(v) if err != nil { - return xerrors.Errorf("failed to read json: %v", err) + return xerrors.Errorf("failed to read json: %w", err) } return nil } @@ -38,12 +38,12 @@ func WriteJSON(ctx context.Context, c *Conn, v interface{}) error { e := json.NewEncoder(w) err := e.Encode(v) if err != nil { - return xerrors.Errorf("failed to write json: %v", err) + return xerrors.Errorf("failed to write json: %w", err) } err = w.Close() if err != nil { - return xerrors.Errorf("failed to write json: %v", err) + return xerrors.Errorf("failed to write json: %w", err) } return nil } diff --git a/opcode.go b/opcode.go index 3f9738d..86f94bd 100644 --- a/opcode.go +++ b/opcode.go @@ -2,6 +2,7 @@ package websocket // opcode represents a WebSocket Opcode. type opcode int + //go:generate go run golang.org/x/tools/cmd/stringer -type=opcode // opcode constants. diff --git a/statuscode.go b/statuscode.go index 306dfdd..40f8609 100644 --- a/statuscode.go +++ b/statuscode.go @@ -12,6 +12,7 @@ import ( // StatusCode represents a WebSocket status code. type StatusCode int + //go:generate go run golang.org/x/tools/cmd/stringer -type=StatusCode // These codes were retrieved from: diff --git a/websocket.go b/websocket.go index 2898a07..d88710f 100644 --- a/websocket.go +++ b/websocket.go @@ -58,7 +58,7 @@ func (c *Conn) getCloseErr() error { func (c *Conn) close(err error) { if err != nil { - err = xerrors.Errorf("websocket: connection broken: %v", err) + err = xerrors.Errorf("websocket: connection broken: %w", err) } c.closeOnce.Do(func() { @@ -102,20 +102,20 @@ func (c *Conn) writeFrame(h header, p []byte) { b2 := marshalHeader(h) _, err := c.bw.Write(b2) if err != nil { - c.close(xerrors.Errorf("failed to write to connection: %v", err)) + c.close(xerrors.Errorf("failed to write to connection: %w", err)) return } _, err = c.bw.Write(p) if err != nil { - c.close(xerrors.Errorf("failed to write to connection: %v", err)) + c.close(xerrors.Errorf("failed to write to connection: %w", err)) return } if h.opcode.controlOp() { err := c.bw.Flush() if err != nil { - c.close(xerrors.Errorf("failed to write to connection: %v", err)) + c.close(xerrors.Errorf("failed to write to connection: %w", err)) return } } @@ -176,7 +176,7 @@ messageLoop: if !ok { err := c.bw.Flush() if err != nil { - c.close(xerrors.Errorf("failed to write to connection: %v", err)) + c.close(xerrors.Errorf("failed to write to connection: %w", err)) return } } @@ -210,7 +210,7 @@ func (c *Conn) handleControl(h header) { b := make([]byte, h.payloadLength) _, err := io.ReadFull(c.br, b) if err != nil { - c.close(xerrors.Errorf("failed to read control frame payload: %v", err)) + c.close(xerrors.Errorf("failed to read control frame payload: %w", err)) return } @@ -226,7 +226,7 @@ func (c *Conn) handleControl(h header) { if len(b) > 0 { code, reason, err := parseClosePayload(b) if err != nil { - c.close(xerrors.Errorf("read invalid close payload: %v", err)) + c.close(xerrors.Errorf("read invalid close payload: %w", err)) return } c.Close(code, reason) @@ -247,7 +247,7 @@ func (c *Conn) readLoop() { for { h, err := readHeader(c.br) if err != nil { - c.close(xerrors.Errorf("failed to read header: %v", err)) + c.close(xerrors.Errorf("failed to read header: %w", err)) return } @@ -298,7 +298,7 @@ func (c *Conn) readLoop() { _, err = io.ReadFull(c.br, b) if err != nil { - c.close(xerrors.Errorf("failed to read from connection: %v", err)) + c.close(xerrors.Errorf("failed to read from connection: %w", err)) return } left -= int64(len(b)) @@ -355,14 +355,14 @@ func (c *Conn) MessageWriter(dataType DataType) *MessageWriter { func (c *Conn) ReadMessage(ctx context.Context) (DataType, *MessageReader, error) { select { case <-c.closed: - return 0, nil, xerrors.Errorf("failed to read message: %v", c.getCloseErr()) + return 0, nil, xerrors.Errorf("failed to read message: %w", c.getCloseErr()) case opcode := <-c.read: return DataType(opcode), &MessageReader{ ctx: context.Background(), c: c, }, nil case <-ctx.Done(): - return 0, nil, xerrors.Errorf("failed to read message: %v", ctx.Err()) + return 0, nil, xerrors.Errorf("failed to read message: %w", ctx.Err()) } } -- GitLab