From 0919bdbf52368af78a7a2b04c3944ac55ea68efc Mon Sep 17 00:00:00 2001 From: Anmol Sethi <hi@nhooyr.io> Date: Sat, 5 Oct 2019 22:23:48 -0400 Subject: [PATCH] Update docs for CloseStatus --- README.md | 4 +++- conn_test.go | 4 ++-- doc.go | 1 + example_test.go | 4 +--- frame.go | 4 ++-- frame_test.go | 2 +- 6 files changed, 10 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 3ab395e..f6afbd8 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,9 @@ go get nhooyr.io/websocket 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). -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). +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). +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). +See the [CloseError godoc example](https://godoc.org/nhooyr.io/websocket#example-CloseError). ### Server diff --git a/conn_test.go b/conn_test.go index 6ef7782..0e012bf 100644 --- a/conn_test.go +++ b/conn_test.go @@ -586,8 +586,8 @@ func TestConn(t *testing.T) { return err } _, _, err = c.Read(ctx) - cerr := &websocket.CloseError{} - if !errors.As(err, cerr) || cerr.Code != websocket.StatusProtocolError { + var cerr websocket.CloseError + if !errors.As(err, &cerr) || cerr.Code != websocket.StatusProtocolError { return fmt.Errorf("expected close error with StatusProtocolError: %+v", err) } return nil diff --git a/doc.go b/doc.go index a17bfb0..1610eed 100644 --- a/doc.go +++ b/doc.go @@ -16,6 +16,7 @@ // comparison with existing implementations. // // Use the errors.As function new in Go 1.13 to check for websocket.CloseError. +// Or use the CloseStatus function to grab the StatusCode out of a websocket.CloseError // See the CloseError example. // // Wasm diff --git a/example_test.go b/example_test.go index 2cedddf..1cb3d79 100644 --- a/example_test.go +++ b/example_test.go @@ -4,7 +4,6 @@ package websocket_test import ( "context" - "errors" "log" "net/http" "time" @@ -76,8 +75,7 @@ func ExampleCloseError() { defer c.Close(websocket.StatusInternalError, "the sky is falling") _, _, err = c.Reader(ctx) - var cerr websocket.CloseError - if !errors.As(err, &cerr) || cerr.Code != websocket.StatusNormalClosure { + if websocket.CloseStatus(err) != websocket.StatusNormalClosure { log.Fatalf("expected to be disconnected with StatusNormalClosure but got: %+v", err) return } diff --git a/frame.go b/frame.go index 796c1c8..b5301d7 100644 --- a/frame.go +++ b/frame.go @@ -253,11 +253,11 @@ func (ce CloseError) Error() string { return fmt.Sprintf("status = %v and reason = %q", ce.Code, ce.Reason) } -// CloseStatus is a convenience wrapper around xerrors.As to grab +// CloseStatus is a convenience wrapper around errors.As to grab // the status code from a *CloseError. If the passed error is nil // or not a *CloseError, the returned StatusCode will be -1. func CloseStatus(err error) StatusCode { - var ce *CloseError + var ce CloseError if errors.As(err, &ce) { return ce.Code } diff --git a/frame_test.go b/frame_test.go index a4fead4..8c5760e 100644 --- a/frame_test.go +++ b/frame_test.go @@ -399,7 +399,7 @@ func TestCloseStatus(t *testing.T) { }, { name: "StatusInternalError", - in: &CloseError{ + in: CloseError{ Code: StatusInternalError, }, exp: StatusInternalError, -- GitLab