We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see ourdocumentation.
There was an error while loading.Please reload this page.
ErrMessageTooBig
1 parentc7846ea commit7d7c644Copy full SHA for 7d7c644
conn_test.go
@@ -421,6 +421,25 @@ func TestConn(t *testing.T) {
421
err=c1.Close(websocket.StatusNormalClosure,"")
422
assert.Success(t,err)
423
})
424
+
425
+t.Run("ReadLimitExceededReturnsErrMessageTooBig",func(t*testing.T) {
426
+tt,c1,c2:=newConnTest(t,nil,nil)
427
428
+c1.SetReadLimit(1024)
429
+_=c2.CloseRead(tt.ctx)
430
431
+writeDone:=xsync.Go(func()error {
432
+payload:=strings.Repeat("x",4096)
433
+returnc2.Write(tt.ctx,websocket.MessageText, []byte(payload))
434
+})
435
436
+_,_,err:=c1.Read(tt.ctx)
437
+assert.ErrorIs(t,websocket.ErrMessageTooBig,err)
438
+assert.Contains(t,err,"read limited at 1025 bytes")
439
440
+_=c2.CloseNow()
441
+<-writeDone
442
443
}
444
445
funcTestWasm(t*testing.T) {
errors.go
@@ -0,0 +1,8 @@
1
+package websocket
2
3
+import (
4
+"errors"
5
+)
6
7
+// ErrMessageTooBig is returned when a message exceeds the read limit.
8
+varErrMessageTooBig=errors.New("websocket: message too big")
read.go
@@ -90,7 +90,8 @@ func (c *Conn) CloseRead(ctx context.Context) context.Context {
90
//
91
// By default, the connection has a message read limit of 32768 bytes.
92
93
-// When the limit is hit, the connection will be closed with StatusMessageTooBig.
+// When the limit is hit, reads return an error wrapping ErrMessageTooBig and
94
+// the connection is closed with StatusMessageTooBig.
95
96
// Set to -1 to disable.
97
func (c*Conn)SetReadLimit(nint64) {
@@ -522,9 +523,9 @@ func (lr *limitReader) Read(p []byte) (int, error) {
522
523
524
525
iflr.n==0 {
-err:=fmt.Errorf("read limited at %v bytes",lr.limit.Load())
526
-lr.c.writeError(StatusMessageTooBig,err)
527
-return0,err
+reason:=fmt.Errorf("read limited at %d bytes",lr.limit.Load())
+lr.c.writeError(StatusMessageTooBig,reason)
528
+return0,fmt.Errorf("%w: %v",ErrMessageTooBig,reason)
529
530
531
ifint64(len(p))>lr.n {
ws_js.go
@@ -144,9 +144,9 @@ func (c *Conn) Read(ctx context.Context) (MessageType, []byte, error) {
144
145
readLimit:=c.msgReadLimit.Load()
146
ifreadLimit>=0&&int64(len(p))>readLimit {
147
-err:=fmt.Errorf("read limited at %v bytes",c.msgReadLimit.Load())
148
-c.Close(StatusMessageTooBig,err.Error())
149
-return0,nil,err
+reason:=fmt.Errorf("read limited at %d bytes",c.msgReadLimit.Load())
+c.Close(StatusMessageTooBig,reason.Error())
+return0,nil,fmt.Errorf("%w: %v",ErrMessageTooBig,reason)
150
151
returntyp,p,nil
152