Skip to content

Commit 83d374e

Browse files
committed
Update docs for CloseStatus
1 parent a78b6d4 commit 83d374e

File tree

4 files changed

+7
-6
lines changed

4 files changed

+7
-6
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ go get nhooyr.io/websocket
3333

3434
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).
3535

36-
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).
36+
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).
37+
There is also [websocket.CloseStatus](https://godoc.org/nhooyr.io/websocket#CloseStatus) to quickly grab the close status code out of a [websocket.CloseError](https://godoc.org/nhooyr.io/websocket#CloseError).
38+
See the [CloseError godoc example](https://godoc.org/nhooyr.io/websocket#example-CloseError).
3739

3840
### Server
3941

doc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// comparison with existing implementations.
1717
//
1818
// Use the errors.As function new in Go 1.13 to check for websocket.CloseError.
19+
// Or use the CloseStatus function to grab the StatusCode out of a websocket.CloseError
1920
// See the CloseError example.
2021
//
2122
// Wasm

example_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package websocket_test
44

55
import (
66
"context"
7-
"errors"
87
"log"
98
"net/http"
109
"time"
@@ -76,8 +75,7 @@ func ExampleCloseError() {
7675
defer c.Close(websocket.StatusInternalError, "the sky is falling")
7776

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

frame.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,11 @@ func (ce CloseError) Error() string {
253253
return fmt.Sprintf("status = %v and reason = %q", ce.Code, ce.Reason)
254254
}
255255

256-
// CloseStatus is a convenience wrapper around xerrors.As to grab
256+
// CloseStatus is a convenience wrapper around errors.As to grab
257257
// the status code from a *CloseError. If the passed error is nil
258258
// or not a *CloseError, the returned StatusCode will be -1.
259259
func CloseStatus(err error) StatusCode {
260-
var ce *CloseError
260+
var ce CloseError
261261
if errors.As(err, &ce) {
262262
return ce.Code
263263
}

0 commit comments

Comments
 (0)