Skip to content

Commit 72c0b8a

Browse files
committed
Fix error wrapping verbs
Updates #46
1 parent 4ad2637 commit 72c0b8a

File tree

7 files changed

+22
-19
lines changed

7 files changed

+22
-19
lines changed

accept.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func Accept(w http.ResponseWriter, r *http.Request, opts ...AcceptOption) (*Conn
118118

119119
netConn, brw, err := hj.Hijack()
120120
if err != nil {
121-
err = xerrors.Errorf("websocket: failed to hijack connection: %v", err)
121+
err = xerrors.Errorf("websocket: failed to hijack connection: %w", err)
122122
http.Error(w, err.Error(), http.StatusInternalServerError)
123123
return nil, err
124124
}
@@ -165,7 +165,7 @@ func authenticateOrigin(r *http.Request, origins []string) error {
165165
}
166166
u, err := url.Parse(origin)
167167
if err != nil {
168-
return xerrors.Errorf("failed to parse Origin header %q: %v", origin, err)
168+
return xerrors.Errorf("failed to parse Origin header %q: %w", origin, err)
169169
}
170170
for _, o := range origins {
171171
if u.Host == o {

datatype.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package websocket
22

33
// DataType represents the Opcode of a WebSocket data frame.
44
type DataType int
5+
56
//go:generate go run golang.org/x/tools/cmd/stringer -type=DataType
67

78
// DataType constants.

dial.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func Dial(ctx context.Context, u string, opts ...DialOption) (_ *Conn, _ *http.R
7474

7575
parsedURL, err := url.Parse(u)
7676
if err != nil {
77-
return nil, nil, xerrors.Errorf("failed to parse websocket url: %v", err)
77+
return nil, nil, xerrors.Errorf("failed to parse websocket url: %w", err)
7878
}
7979

8080
switch parsedURL.Scheme {
@@ -113,7 +113,7 @@ func Dial(ctx context.Context, u string, opts ...DialOption) (_ *Conn, _ *http.R
113113
}()
114114

115115
if resp.StatusCode != http.StatusSwitchingProtocols {
116-
return nil, resp, xerrors.Errorf("websocket: expected status code %v but got %v", http.StatusSwitchingProtocols)
116+
return nil, resp, xerrors.Errorf("websocket: expected status code %v but got %v", http.StatusSwitchingProtocols, resp.StatusCode)
117117
}
118118

119119
if !httpguts.HeaderValuesContainsToken(resp.Header["Connection"], "Upgrade") {

json.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
func ReadJSON(ctx context.Context, c *Conn, v interface{}) error {
1313
typ, r, err := c.ReadMessage(ctx)
1414
if err != nil {
15-
return xerrors.Errorf("failed to read json: %v", err)
15+
return xerrors.Errorf("failed to read json: %w", err)
1616
}
1717

1818
if typ != websocket.TextFrame {
@@ -25,7 +25,7 @@ func ReadJSON(ctx context.Context, c *Conn, v interface{}) error {
2525
d := json.NewDecoder(r)
2626
err = d.Decode(v)
2727
if err != nil {
28-
return xerrors.Errorf("failed to read json: %v", err)
28+
return xerrors.Errorf("failed to read json: %w", err)
2929
}
3030
return nil
3131
}
@@ -38,12 +38,12 @@ func WriteJSON(ctx context.Context, c *Conn, v interface{}) error {
3838
e := json.NewEncoder(w)
3939
err := e.Encode(v)
4040
if err != nil {
41-
return xerrors.Errorf("failed to write json: %v", err)
41+
return xerrors.Errorf("failed to write json: %w", err)
4242
}
4343

4444
err = w.Close()
4545
if err != nil {
46-
return xerrors.Errorf("failed to write json: %v", err)
46+
return xerrors.Errorf("failed to write json: %w", err)
4747
}
4848
return nil
4949
}

opcode.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package websocket
22

33
// opcode represents a WebSocket Opcode.
44
type opcode int
5+
56
//go:generate go run golang.org/x/tools/cmd/stringer -type=opcode
67

78
// opcode constants.

statuscode.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
// StatusCode represents a WebSocket status code.
1414
type StatusCode int
15+
1516
//go:generate go run golang.org/x/tools/cmd/stringer -type=StatusCode
1617

1718
// These codes were retrieved from:

websocket.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (c *Conn) getCloseErr() error {
5858

5959
func (c *Conn) close(err error) {
6060
if err != nil {
61-
err = xerrors.Errorf("websocket: connection broken: %v", err)
61+
err = xerrors.Errorf("websocket: connection broken: %w", err)
6262
}
6363

6464
c.closeOnce.Do(func() {
@@ -102,20 +102,20 @@ func (c *Conn) writeFrame(h header, p []byte) {
102102
b2 := marshalHeader(h)
103103
_, err := c.bw.Write(b2)
104104
if err != nil {
105-
c.close(xerrors.Errorf("failed to write to connection: %v", err))
105+
c.close(xerrors.Errorf("failed to write to connection: %w", err))
106106
return
107107
}
108108

109109
_, err = c.bw.Write(p)
110110
if err != nil {
111-
c.close(xerrors.Errorf("failed to write to connection: %v", err))
111+
c.close(xerrors.Errorf("failed to write to connection: %w", err))
112112
return
113113
}
114114

115115
if h.opcode.controlOp() {
116116
err := c.bw.Flush()
117117
if err != nil {
118-
c.close(xerrors.Errorf("failed to write to connection: %v", err))
118+
c.close(xerrors.Errorf("failed to write to connection: %w", err))
119119
return
120120
}
121121
}
@@ -176,7 +176,7 @@ messageLoop:
176176
if !ok {
177177
err := c.bw.Flush()
178178
if err != nil {
179-
c.close(xerrors.Errorf("failed to write to connection: %v", err))
179+
c.close(xerrors.Errorf("failed to write to connection: %w", err))
180180
return
181181
}
182182
}
@@ -210,7 +210,7 @@ func (c *Conn) handleControl(h header) {
210210
b := make([]byte, h.payloadLength)
211211
_, err := io.ReadFull(c.br, b)
212212
if err != nil {
213-
c.close(xerrors.Errorf("failed to read control frame payload: %v", err))
213+
c.close(xerrors.Errorf("failed to read control frame payload: %w", err))
214214
return
215215
}
216216

@@ -226,7 +226,7 @@ func (c *Conn) handleControl(h header) {
226226
if len(b) > 0 {
227227
code, reason, err := parseClosePayload(b)
228228
if err != nil {
229-
c.close(xerrors.Errorf("read invalid close payload: %v", err))
229+
c.close(xerrors.Errorf("read invalid close payload: %w", err))
230230
return
231231
}
232232
c.Close(code, reason)
@@ -247,7 +247,7 @@ func (c *Conn) readLoop() {
247247
for {
248248
h, err := readHeader(c.br)
249249
if err != nil {
250-
c.close(xerrors.Errorf("failed to read header: %v", err))
250+
c.close(xerrors.Errorf("failed to read header: %w", err))
251251
return
252252
}
253253

@@ -298,7 +298,7 @@ func (c *Conn) readLoop() {
298298

299299
_, err = io.ReadFull(c.br, b)
300300
if err != nil {
301-
c.close(xerrors.Errorf("failed to read from connection: %v", err))
301+
c.close(xerrors.Errorf("failed to read from connection: %w", err))
302302
return
303303
}
304304
left -= int64(len(b))
@@ -355,14 +355,14 @@ func (c *Conn) MessageWriter(dataType DataType) *MessageWriter {
355355
func (c *Conn) ReadMessage(ctx context.Context) (DataType, *MessageReader, error) {
356356
select {
357357
case <-c.closed:
358-
return 0, nil, xerrors.Errorf("failed to read message: %v", c.getCloseErr())
358+
return 0, nil, xerrors.Errorf("failed to read message: %w", c.getCloseErr())
359359
case opcode := <-c.read:
360360
return DataType(opcode), &MessageReader{
361361
ctx: context.Background(),
362362
c: c,
363363
}, nil
364364
case <-ctx.Done():
365-
return 0, nil, xerrors.Errorf("failed to read message: %v", ctx.Err())
365+
return 0, nil, xerrors.Errorf("failed to read message: %w", ctx.Err())
366366
}
367367
}
368368

0 commit comments

Comments
 (0)