From e96e02a00642fcc64ac10b2aa0eb8c7174bddff1 Mon Sep 17 00:00:00 2001 From: Danielle Maywood Date: Fri, 15 Aug 2025 12:01:15 +0000 Subject: [PATCH 1/2] refactor: replace fmt.Errorf with MessageTooBigError --- read.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/read.go b/read.go index aab9e141..672e8f36 100644 --- a/read.go +++ b/read.go @@ -17,6 +17,14 @@ import ( "github.com/coder/websocket/internal/util" ) +type MessageTooBigError struct { + Limit int64 +} + +func (e MessageTooBigError) Error() string { + return fmt.Sprintf("read limited at %v bytes", e.Limit) +} + // 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 +528,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 := MessageTooBigError{Limit: lr.limit.Load()} lr.c.writeError(StatusMessageTooBig, err) return 0, err } From c44f12f2ac7c6363176eef98f1c96fbb572bf9b7 Mon Sep 17 00:00:00 2001 From: Danielle Maywood Date: Fri, 15 Aug 2025 14:06:19 +0000 Subject: [PATCH 2/2] refactor: replace MessageTooBigError with ErrMessageTooBig --- read.go | 10 ++-------- ws_js.go | 4 +++- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/read.go b/read.go index 672e8f36..85e09893 100644 --- a/read.go +++ b/read.go @@ -17,13 +17,7 @@ import ( "github.com/coder/websocket/internal/util" ) -type MessageTooBigError struct { - Limit int64 -} - -func (e MessageTooBigError) Error() string { - return fmt.Sprintf("read limited at %v bytes", e.Limit) -} +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. @@ -528,7 +522,7 @@ func (lr *limitReader) Read(p []byte) (int, error) { } if lr.n == 0 { - err := MessageTooBigError{Limit: 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 }