Skip to content

Commit ef0e551

Browse files
committed
redirect all exceptions thrown from websocket-callbacks to onError instead of shouting down the client/server (TooTallNate#122)
1 parent 9a8fe9a commit ef0e551

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

src/main/java/org/java_websocket/WebSocketImpl.java

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
*
4545
*/
4646
public class WebSocketImpl extends WebSocket {
47-
47+
4848
public static final List<Draft> defaultdraftlist = new ArrayList<Draft>( 4 );
4949
static {
5050
defaultdraftlist.add( new Draft_17() );
@@ -217,6 +217,10 @@ private boolean decodeHandshake( ByteBuffer socketBufferNew ) throws IOException
217217
} catch ( InvalidDataException e ) {
218218
flushAndClose( e.getCloseCode(), e.getMessage(), false );
219219
return false;
220+
} catch ( RuntimeException e ) {
221+
wsl.onWebsocketError( this, e );
222+
flushAndClose( CloseFrame.NEVERCONNECTED, e.getMessage(), false );
223+
return false;
220224
}
221225
write( d.createHandshake( d.postProcessHandshakeResponseAsServer( handshake, response ), role ) );
222226
draft = d;
@@ -264,6 +268,10 @@ private boolean decodeHandshake( ByteBuffer socketBufferNew ) throws IOException
264268
} catch ( InvalidDataException e ) {
265269
flushAndClose( e.getCloseCode(), e.getMessage(), false );
266270
return false;
271+
} catch ( RuntimeException e ) {
272+
wsl.onWebsocketError( this, e );
273+
flushAndClose( CloseFrame.NEVERCONNECTED, e.getMessage(), false );
274+
return false;
267275
}
268276
open( handshake );
269277
return true;
@@ -374,8 +382,13 @@ private void close( int code, String message, boolean remote ) {
374382
}
375383
if( draft.getCloseHandshakeType() != CloseHandshakeType.NONE ) {
376384
try {
377-
if( !remote )
378-
wsl.onWebsocketCloseInitiated( this, code, message );
385+
if( !remote ) {
386+
try {
387+
wsl.onWebsocketCloseInitiated( this, code, message );
388+
} catch ( RuntimeException e ) {
389+
wsl.onWebsocketError( this, e );
390+
}
391+
}
379392
sendFrame( new CloseFrameBuilder( code, message ) );
380393
} catch ( InvalidDataException e ) {
381394
wsl.onWebsocketError( this, e );
@@ -425,7 +438,11 @@ protected synchronized void closeConnection( int code, String message, boolean r
425438
wsl.onWebsocketError( this, e );
426439
}
427440
}
428-
this.wsl.onWebsocketClose( this, code, message, remote );
441+
try {
442+
this.wsl.onWebsocketClose( this, code, message, remote );
443+
} catch ( RuntimeException e ) {
444+
wsl.onWebsocketError( this, e );
445+
}
429446
if( draft != null )
430447
draft.reset();
431448
tempContiniousFrame = null;
@@ -461,8 +478,11 @@ protected synchronized void flushAndClose( int code, String message, boolean rem
461478
flushandclosestate = true;
462479

463480
wsl.onWriteDemand( this ); // ensures that all outgoing frames are flushed before closing the connection
464-
465-
this.wsl.onWebsocketClosing( this, code, message, remote );
481+
try {
482+
wsl.onWebsocketClosing( this, code, message, remote );
483+
} catch ( RuntimeException e ) {
484+
wsl.onWebsocketError( this, e );
485+
}
466486
if( draft != null )
467487
draft.reset();
468488
tempContiniousFrame = null;
@@ -577,6 +597,9 @@ public void startHandshake( ClientHandshakeBuilder handshakedata ) throws Invali
577597
} catch ( InvalidDataException e ) {
578598
// Stop if the client code throws an exception
579599
throw new InvalidHandshakeException( "Handshake data rejected by client." );
600+
} catch ( RuntimeException e ) {
601+
wsl.onWebsocketError( this, e );
602+
throw new InvalidHandshakeException( "rejected because of" + e );
580603
}
581604

582605
// Send
@@ -624,7 +647,11 @@ private void open( Handshakedata d ) throws IOException {
624647
if( DEBUG )
625648
System.out.println( "open using draft: " + draft.getClass().getSimpleName() );
626649
handshakeComplete = true;
627-
wsl.onWebsocketOpen( this, d );
650+
try {
651+
wsl.onWebsocketOpen( this, d );
652+
} catch ( RuntimeException e ) {
653+
wsl.onWebsocketError( this, e );
654+
}
628655
}
629656

630657
@Override

0 commit comments

Comments
 (0)