diff --git a/read.go b/read.go index aab9e141..85e09893 100644 --- a/read.go +++ b/read.go @@ -17,6 +17,8 @@ import ( "github.com/coder/websocket/internal/util" ) +var ErrMessageTooBig = errors.New("websocket: message too big") + // Reader reads from the connection until there is a WebSocket // data message to be read. It will handle ping, pong and close frames as appropriate. // @@ -520,7 +522,7 @@ func (lr *limitReader) Read(p []byte) (int, error) { } if lr.n == 0 { - err := fmt.Errorf("read limited at %v bytes", lr.limit.Load()) + err := fmt.Errorf("%w: %d bytes", ErrMessageTooBig, lr.limit.Load()) lr.c.writeError(StatusMessageTooBig, err) return 0, err } diff --git a/ws_js.go b/ws_js.go index 8d52aeab..2381fe13 100644 --- a/ws_js.go +++ b/ws_js.go @@ -19,6 +19,8 @@ import ( "github.com/coder/websocket/internal/wsjs" ) +var ErrMessageTooBig = errors.New("websocket: message too big") + // opcode represents a WebSocket opcode. type opcode int @@ -144,7 +146,7 @@ func (c *Conn) Read(ctx context.Context) (MessageType, []byte, error) { } readLimit := c.msgReadLimit.Load() if readLimit >= 0 && int64(len(p)) > readLimit { - err := fmt.Errorf("read limited at %v bytes", c.msgReadLimit.Load()) + err := fmt.Errorf("%w: %d bytes", ErrMessageTooBig, c.msgReadLimit.Load()) c.Close(StatusMessageTooBig, err.Error()) return 0, nil, err }