Skip to content

refactor: replace fmt.Errorf with MessageTooBigError #535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion read.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion ws_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
}
Expand Down