Skip to content

Commit c44f12f

Browse files
refactor: replace MessageTooBigError with ErrMessageTooBig
1 parent e96e02a commit c44f12f

File tree

2 files changed

+5
-9
lines changed

2 files changed

+5
-9
lines changed

read.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,7 @@ import (
1717
"github.com/coder/websocket/internal/util"
1818
)
1919

20-
type MessageTooBigError struct {
21-
Limit int64
22-
}
23-
24-
func (e MessageTooBigError) Error() string {
25-
return fmt.Sprintf("read limited at %v bytes", e.Limit)
26-
}
20+
var ErrMessageTooBig = errors.New("websocket: message too big")
2721

2822
// Reader reads from the connection until there is a WebSocket
2923
// 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) {
528522
}
529523

530524
if lr.n == 0 {
531-
err := MessageTooBigError{Limit: lr.limit.Load()}
525+
err := fmt.Errorf("%w: %d bytes", ErrMessageTooBig, lr.limit.Load())
532526
lr.c.writeError(StatusMessageTooBig, err)
533527
return 0, err
534528
}

ws_js.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
"github.com/coder/websocket/internal/wsjs"
2020
)
2121

22+
var ErrMessageTooBig = errors.New("websocket: message too big")
23+
2224
// opcode represents a WebSocket opcode.
2325
type opcode int
2426

@@ -144,7 +146,7 @@ func (c *Conn) Read(ctx context.Context) (MessageType, []byte, error) {
144146
}
145147
readLimit := c.msgReadLimit.Load()
146148
if readLimit >= 0 && int64(len(p)) > readLimit {
147-
err := fmt.Errorf("read limited at %v bytes", c.msgReadLimit.Load())
149+
err := fmt.Errorf("%w: %d bytes", ErrMessageTooBig, c.msgReadLimit.Load())
148150
c.Close(StatusMessageTooBig, err.Error())
149151
return 0, nil, err
150152
}

0 commit comments

Comments
 (0)