Skip to content

Commit 7235d8b

Browse files
committed
Remove xerrors dependency
Closes #134
1 parent 6d3f05d commit 7235d8b

14 files changed

+115
-126
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ go get nhooyr.io/websocket@v1.5.0
3232

3333
For a production quality example that shows off the full API, see the [echo example on the godoc](https://godoc.org/nhooyr.io/websocket#example-package--Echo). On github, the example is at [example_echo_test.go](./example_echo_test.go).
3434

35-
Please use the [golang.org/x/xerrors.As](https://godoc.org/golang.org/x/xerrors#As) package to check for [websocket.CloseError](https://godoc.org/nhooyr.io/websocket#CloseError). See the [CloseError godoc example](https://godoc.org/nhooyr.io/websocket#example-CloseError).
35+
Please use the [errors.As](https://golang.org/pkg/errors/#As) function [new in Go 1.13](https://golang.org/doc/go1.13#error_wrapping) to check for [websocket.CloseError](https://godoc.org/nhooyr.io/websocket#CloseError). See the [CloseError godoc example](https://godoc.org/nhooyr.io/websocket#example-CloseError).
3636

3737
### Server
3838

accept.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import (
44
"bytes"
55
"crypto/sha1"
66
"encoding/base64"
7+
"errors"
8+
"fmt"
79
"io"
810
"net/http"
911
"net/textproto"
1012
"net/url"
1113
"strings"
12-
13-
"golang.org/x/xerrors"
1414
)
1515

1616
// AcceptOptions represents the options available to pass to Accept.
@@ -42,31 +42,31 @@ type AcceptOptions struct {
4242

4343
func verifyClientRequest(w http.ResponseWriter, r *http.Request) error {
4444
if !headerValuesContainsToken(r.Header, "Connection", "Upgrade") {
45-
err := xerrors.Errorf("websocket protocol violation: Connection header %q does not contain Upgrade", r.Header.Get("Connection"))
45+
err := fmt.Errorf("websocket protocol violation: Connection header %q does not contain Upgrade", r.Header.Get("Connection"))
4646
http.Error(w, err.Error(), http.StatusBadRequest)
4747
return err
4848
}
4949

5050
if !headerValuesContainsToken(r.Header, "Upgrade", "WebSocket") {
51-
err := xerrors.Errorf("websocket protocol violation: Upgrade header %q does not contain websocket", r.Header.Get("Upgrade"))
51+
err := fmt.Errorf("websocket protocol violation: Upgrade header %q does not contain websocket", r.Header.Get("Upgrade"))
5252
http.Error(w, err.Error(), http.StatusBadRequest)
5353
return err
5454
}
5555

5656
if r.Method != "GET" {
57-
err := xerrors.Errorf("websocket protocol violation: handshake request method is not GET but %q", r.Method)
57+
err := fmt.Errorf("websocket protocol violation: handshake request method is not GET but %q", r.Method)
5858
http.Error(w, err.Error(), http.StatusBadRequest)
5959
return err
6060
}
6161

6262
if r.Header.Get("Sec-WebSocket-Version") != "13" {
63-
err := xerrors.Errorf("unsupported websocket protocol version (only 13 is supported): %q", r.Header.Get("Sec-WebSocket-Version"))
63+
err := fmt.Errorf("unsupported websocket protocol version (only 13 is supported): %q", r.Header.Get("Sec-WebSocket-Version"))
6464
http.Error(w, err.Error(), http.StatusBadRequest)
6565
return err
6666
}
6767

6868
if r.Header.Get("Sec-WebSocket-Key") == "" {
69-
err := xerrors.New("websocket protocol violation: missing Sec-WebSocket-Key")
69+
err := errors.New("websocket protocol violation: missing Sec-WebSocket-Key")
7070
http.Error(w, err.Error(), http.StatusBadRequest)
7171
return err
7272
}
@@ -86,7 +86,7 @@ func verifyClientRequest(w http.ResponseWriter, r *http.Request) error {
8686
func Accept(w http.ResponseWriter, r *http.Request, opts *AcceptOptions) (*Conn, error) {
8787
c, err := accept(w, r, opts)
8888
if err != nil {
89-
return nil, xerrors.Errorf("failed to accept websocket connection: %w", err)
89+
return nil, fmt.Errorf("failed to accept websocket connection: %w", err)
9090
}
9191
return c, nil
9292
}
@@ -111,7 +111,7 @@ func accept(w http.ResponseWriter, r *http.Request, opts *AcceptOptions) (*Conn,
111111

112112
hj, ok := w.(http.Hijacker)
113113
if !ok {
114-
err = xerrors.New("passed ResponseWriter does not implement http.Hijacker")
114+
err = errors.New("passed ResponseWriter does not implement http.Hijacker")
115115
http.Error(w, http.StatusText(http.StatusNotImplemented), http.StatusNotImplemented)
116116
return nil, err
117117
}
@@ -130,7 +130,7 @@ func accept(w http.ResponseWriter, r *http.Request, opts *AcceptOptions) (*Conn,
130130

131131
netConn, brw, err := hj.Hijack()
132132
if err != nil {
133-
err = xerrors.Errorf("failed to hijack connection: %w", err)
133+
err = fmt.Errorf("failed to hijack connection: %w", err)
134134
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
135135
return nil, err
136136
}
@@ -206,10 +206,10 @@ func authenticateOrigin(r *http.Request) error {
206206
}
207207
u, err := url.Parse(origin)
208208
if err != nil {
209-
return xerrors.Errorf("failed to parse Origin header %q: %w", origin, err)
209+
return fmt.Errorf("failed to parse Origin header %q: %w", origin, err)
210210
}
211211
if strings.EqualFold(u.Host, r.Host) {
212212
return nil
213213
}
214-
return xerrors.Errorf("request Origin %q is not authorized for Host %q", origin, r.Host)
214+
return fmt.Errorf("request Origin %q is not authorized for Host %q", origin, r.Host)
215215
}

dial.go

+11-12
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ import (
55
"bytes"
66
"context"
77
"encoding/base64"
8+
"fmt"
89
"io"
910
"io/ioutil"
1011
"math/rand"
1112
"net/http"
1213
"net/url"
1314
"strings"
1415
"sync"
15-
16-
"golang.org/x/xerrors"
1716
)
1817

1918
// DialOptions represents the options available to pass to Dial.
@@ -44,7 +43,7 @@ type DialOptions struct {
4443
func Dial(ctx context.Context, u string, opts *DialOptions) (*Conn, *http.Response, error) {
4544
c, r, err := dial(ctx, u, opts)
4645
if err != nil {
47-
return nil, r, xerrors.Errorf("failed to websocket dial: %w", err)
46+
return nil, r, fmt.Errorf("failed to websocket dial: %w", err)
4847
}
4948
return c, r, nil
5049
}
@@ -62,15 +61,15 @@ func dial(ctx context.Context, u string, opts *DialOptions) (_ *Conn, _ *http.Re
6261
opts.HTTPClient = http.DefaultClient
6362
}
6463
if opts.HTTPClient.Timeout > 0 {
65-
return nil, nil, xerrors.Errorf("please use context for cancellation instead of http.Client.Timeout; see https://github.com/nhooyr/websocket/issues/67")
64+
return nil, nil, fmt.Errorf("please use context for cancellation instead of http.Client.Timeout; see https://github.com/nhooyr/websocket/issues/67")
6665
}
6766
if opts.HTTPHeader == nil {
6867
opts.HTTPHeader = http.Header{}
6968
}
7069

7170
parsedURL, err := url.Parse(u)
7271
if err != nil {
73-
return nil, nil, xerrors.Errorf("failed to parse url: %w", err)
72+
return nil, nil, fmt.Errorf("failed to parse url: %w", err)
7473
}
7574

7675
switch parsedURL.Scheme {
@@ -79,7 +78,7 @@ func dial(ctx context.Context, u string, opts *DialOptions) (_ *Conn, _ *http.Re
7978
case "wss":
8079
parsedURL.Scheme = "https"
8180
default:
82-
return nil, nil, xerrors.Errorf("unexpected url scheme: %q", parsedURL.Scheme)
81+
return nil, nil, fmt.Errorf("unexpected url scheme: %q", parsedURL.Scheme)
8382
}
8483

8584
req, _ := http.NewRequest("GET", parsedURL.String(), nil)
@@ -95,7 +94,7 @@ func dial(ctx context.Context, u string, opts *DialOptions) (_ *Conn, _ *http.Re
9594

9695
resp, err := opts.HTTPClient.Do(req)
9796
if err != nil {
98-
return nil, nil, xerrors.Errorf("failed to send handshake request: %w", err)
97+
return nil, nil, fmt.Errorf("failed to send handshake request: %w", err)
9998
}
10099
defer func() {
101100
if err != nil {
@@ -114,7 +113,7 @@ func dial(ctx context.Context, u string, opts *DialOptions) (_ *Conn, _ *http.Re
114113

115114
rwc, ok := resp.Body.(io.ReadWriteCloser)
116115
if !ok {
117-
return nil, resp, xerrors.Errorf("response body is not a io.ReadWriteCloser: %T", rwc)
116+
return nil, resp, fmt.Errorf("response body is not a io.ReadWriteCloser: %T", rwc)
118117
}
119118

120119
c := &Conn{
@@ -132,19 +131,19 @@ func dial(ctx context.Context, u string, opts *DialOptions) (_ *Conn, _ *http.Re
132131

133132
func verifyServerResponse(r *http.Request, resp *http.Response) error {
134133
if resp.StatusCode != http.StatusSwitchingProtocols {
135-
return xerrors.Errorf("expected handshake response status code %v but got %v", http.StatusSwitchingProtocols, resp.StatusCode)
134+
return fmt.Errorf("expected handshake response status code %v but got %v", http.StatusSwitchingProtocols, resp.StatusCode)
136135
}
137136

138137
if !headerValuesContainsToken(resp.Header, "Connection", "Upgrade") {
139-
return xerrors.Errorf("websocket protocol violation: Connection header %q does not contain Upgrade", resp.Header.Get("Connection"))
138+
return fmt.Errorf("websocket protocol violation: Connection header %q does not contain Upgrade", resp.Header.Get("Connection"))
140139
}
141140

142141
if !headerValuesContainsToken(resp.Header, "Upgrade", "WebSocket") {
143-
return xerrors.Errorf("websocket protocol violation: Upgrade header %q does not contain websocket", resp.Header.Get("Upgrade"))
142+
return fmt.Errorf("websocket protocol violation: Upgrade header %q does not contain websocket", resp.Header.Get("Upgrade"))
144143
}
145144

146145
if resp.Header.Get("Sec-WebSocket-Accept") != secWebSocketAccept(r.Header.Get("Sec-WebSocket-Key")) {
147-
return xerrors.Errorf("websocket protocol violation: invalid Sec-WebSocket-Accept %q, key %q",
146+
return fmt.Errorf("websocket protocol violation: invalid Sec-WebSocket-Accept %q, key %q",
148147
resp.Header.Get("Sec-WebSocket-Accept"),
149148
r.Header.Get("Sec-WebSocket-Key"),
150149
)

example_echo_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"time"
1111

1212
"golang.org/x/time/rate"
13-
"golang.org/x/xerrors"
1413

1514
"nhooyr.io/websocket"
1615
"nhooyr.io/websocket/wsjson"
@@ -78,14 +77,14 @@ func echoServer(w http.ResponseWriter, r *http.Request) error {
7877

7978
if c.Subprotocol() != "echo" {
8079
c.Close(websocket.StatusPolicyViolation, "client must speak the echo subprotocol")
81-
return xerrors.Errorf("client does not speak echo sub protocol")
80+
return fmt.Errorf("client does not speak echo sub protocol")
8281
}
8382

8483
l := rate.NewLimiter(rate.Every(time.Millisecond*100), 10)
8584
for {
8685
err = echo(r.Context(), c, l)
8786
if err != nil {
88-
return xerrors.Errorf("failed to echo with %v: %w", r.RemoteAddr, err)
87+
return fmt.Errorf("failed to echo with %v: %w", r.RemoteAddr, err)
8988
}
9089
}
9190
}
@@ -114,7 +113,7 @@ func echo(ctx context.Context, c *websocket.Conn, l *rate.Limiter) error {
114113

115114
_, err = io.Copy(w, r)
116115
if err != nil {
117-
return xerrors.Errorf("failed to io.Copy: %w", err)
116+
return fmt.Errorf("failed to io.Copy: %w", err)
118117
}
119118

120119
err = w.Close()

example_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ package websocket_test
22

33
import (
44
"context"
5+
"errors"
56
"log"
67
"net/http"
78
"time"
89

9-
"golang.org/x/xerrors"
10-
1110
"nhooyr.io/websocket"
1211
"nhooyr.io/websocket/wsjson"
1312
)
@@ -76,7 +75,7 @@ func ExampleCloseError() {
7675

7776
_, _, err = c.Reader(ctx)
7877
var cerr websocket.CloseError
79-
if !xerrors.As(err, &cerr) || cerr.Code != websocket.StatusNormalClosure {
78+
if !errors.As(err, &cerr) || cerr.Code != websocket.StatusNormalClosure {
8079
log.Fatalf("expected to be disconnected with StatusNormalClosure but got: %+v", err)
8180
return
8281
}

export_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ package websocket
33
import (
44
"bufio"
55
"context"
6-
7-
"golang.org/x/xerrors"
6+
"fmt"
87
)
98

109
type (
@@ -65,7 +64,7 @@ func (c *Conn) WriteHeader(ctx context.Context, h Header) error {
6564
})
6665
_, err := c.bw.Write(headerBytes)
6766
if err != nil {
68-
return xerrors.Errorf("failed to write header: %w", err)
67+
return fmt.Errorf("failed to write header: %w", err)
6968
}
7069
if h.Fin {
7170
err = c.Flush()

go.mod

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module nhooyr.io/websocket
22

3-
go 1.12
3+
go 1.13
44

55
require (
66
github.com/fatih/color v1.7.0 // indirect
@@ -21,7 +21,6 @@ require (
2121
golang.org/x/sys v0.0.0-20190830142957-1e83adbbebd0 // indirect
2222
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4
2323
golang.org/x/tools v0.0.0-20190830223141-573d9926052a
24-
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
2524
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
2625
gotest.tools v2.2.0+incompatible // indirect
2726
gotest.tools/gotestsum v0.3.6-0.20190825182939-fc6cb5870c52

header.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"fmt"
66
"io"
77
"math"
8-
9-
"golang.org/x/xerrors"
108
)
119

1210
// First byte contains fin, rsv1, rsv2, rsv3.
@@ -145,7 +143,7 @@ func readHeader(b []byte, r io.Reader) (header, error) {
145143
case payloadLength == 127:
146144
h.payloadLength = int64(binary.BigEndian.Uint64(b))
147145
if h.payloadLength < 0 {
148-
return header{}, xerrors.Errorf("header with negative payload length: %v", h.payloadLength)
146+
return header{}, fmt.Errorf("header with negative payload length: %v", h.payloadLength)
149147
}
150148
b = b[8:]
151149
}

netconn.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ package websocket
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"io"
78
"math"
89
"net"
910
"time"
10-
11-
"golang.org/x/xerrors"
1211
)
1312

1413
// NetConn converts a *websocket.Conn into a net.Conn.
@@ -97,7 +96,7 @@ func (c *netConn) Read(p []byte) (int, error) {
9796
typ, r, err := c.c.Reader(c.readContext)
9897
if err != nil {
9998
var ce CloseError
100-
if xerrors.As(err, &ce) && (ce.Code == StatusNormalClosure) || (ce.Code == StatusGoingAway) {
99+
if errors.As(err, &ce) && (ce.Code == StatusNormalClosure) || (ce.Code == StatusGoingAway) {
101100
c.eofed = true
102101
return 0, io.EOF
103102
}

statuscode.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package websocket
33
import (
44
"encoding/binary"
55
"fmt"
6-
7-
"golang.org/x/xerrors"
86
)
97

108
// StatusCode represents a WebSocket status code.
@@ -43,7 +41,8 @@ const (
4341
// CloseError represents a WebSocket close frame.
4442
// It is returned by Conn's methods when a WebSocket close frame is received from
4543
// the peer.
46-
// You will need to use https://godoc.org/golang.org/x/xerrors#As to check for this error.
44+
// You will need to use the https://golang.org/pkg/errors/#As function, new in Go 1.13,
45+
// to check for this error. See the CloseError example.
4746
type CloseError struct {
4847
Code StatusCode
4948
Reason string
@@ -61,7 +60,7 @@ func parseClosePayload(p []byte) (CloseError, error) {
6160
}
6261

6362
if len(p) < 2 {
64-
return CloseError{}, xerrors.Errorf("close payload %q too small, cannot even contain the 2 byte status code", p)
63+
return CloseError{}, fmt.Errorf("close payload %q too small, cannot even contain the 2 byte status code", p)
6564
}
6665

6766
ce := CloseError{
@@ -70,7 +69,7 @@ func parseClosePayload(p []byte) (CloseError, error) {
7069
}
7170

7271
if !validWireCloseCode(ce.Code) {
73-
return CloseError{}, xerrors.Errorf("invalid status code %v", ce.Code)
72+
return CloseError{}, fmt.Errorf("invalid status code %v", ce.Code)
7473
}
7574

7675
return ce, nil
@@ -98,10 +97,10 @@ const maxControlFramePayload = 125
9897

9998
func (ce CloseError) bytes() ([]byte, error) {
10099
if len(ce.Reason) > maxControlFramePayload-2 {
101-
return nil, xerrors.Errorf("reason string max is %v but got %q with length %v", maxControlFramePayload-2, ce.Reason, len(ce.Reason))
100+
return nil, fmt.Errorf("reason string max is %v but got %q with length %v", maxControlFramePayload-2, ce.Reason, len(ce.Reason))
102101
}
103102
if !validWireCloseCode(ce.Code) {
104-
return nil, xerrors.Errorf("status code %v cannot be set", ce.Code)
103+
return nil, fmt.Errorf("status code %v cannot be set", ce.Code)
105104
}
106105

107106
buf := make([]byte, 2+len(ce.Reason))

0 commit comments

Comments
 (0)