Skip to content

Commit

Permalink
Refine Throwable handling in spring-websocket
Browse files Browse the repository at this point in the history
This commit lowers the level of Throwable handling in parts of
spring-websocket which now handle Exception instead and allow any Error
to propagate.

Closes gh-24075
  • Loading branch information
rstoyanchev committed Nov 26, 2019
1 parent 70a3dbf commit 526d89e
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 42 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -71,7 +71,7 @@ public void onWebSocketConnect(Session session) {
this.wsSession.initializeNativeSession(session);
this.webSocketHandler.afterConnectionEstablished(this.wsSession);
}
catch (Throwable ex) {
catch (Exception ex) {
ExceptionWebSocketHandlerDecorator.tryCloseWithError(this.wsSession, ex, logger);
}
}
Expand All @@ -82,7 +82,7 @@ public void onWebSocketText(String payload) {
try {
this.webSocketHandler.handleMessage(this.wsSession, message);
}
catch (Throwable ex) {
catch (Exception ex) {
ExceptionWebSocketHandlerDecorator.tryCloseWithError(this.wsSession, ex, logger);
}
}
Expand All @@ -93,7 +93,7 @@ public void onWebSocketBinary(byte[] payload, int offset, int length) {
try {
this.webSocketHandler.handleMessage(this.wsSession, message);
}
catch (Throwable ex) {
catch (Exception ex) {
ExceptionWebSocketHandlerDecorator.tryCloseWithError(this.wsSession, ex, logger);
}
}
Expand All @@ -106,7 +106,7 @@ public void onWebSocketFrame(Frame frame) {
try {
this.webSocketHandler.handleMessage(this.wsSession, message);
}
catch (Throwable ex) {
catch (Exception ex) {
ExceptionWebSocketHandlerDecorator.tryCloseWithError(this.wsSession, ex, logger);
}
}
Expand All @@ -118,7 +118,7 @@ public void onWebSocketClose(int statusCode, String reason) {
try {
this.webSocketHandler.afterConnectionClosed(this.wsSession, closeStatus);
}
catch (Throwable ex) {
catch (Exception ex) {
if (logger.isWarnEnabled()) {
logger.warn("Unhandled exception after connection closed for " + this, ex);
}
Expand All @@ -130,7 +130,7 @@ public void onWebSocketError(Throwable cause) {
try {
this.webSocketHandler.handleTransportError(this.wsSession, cause);
}
catch (Throwable ex) {
catch (Exception ex) {
ExceptionWebSocketHandlerDecorator.tryCloseWithError(this.wsSession, ex, logger);
}
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -103,7 +103,7 @@ public void onMessage(javax.websocket.PongMessage message) {
try {
this.handler.afterConnectionEstablished(this.wsSession);
}
catch (Throwable ex) {
catch (Exception ex) {
ExceptionWebSocketHandlerDecorator.tryCloseWithError(this.wsSession, ex, logger);
}
}
Expand All @@ -113,7 +113,7 @@ private void handleTextMessage(javax.websocket.Session session, String payload,
try {
this.handler.handleMessage(this.wsSession, textMessage);
}
catch (Throwable ex) {
catch (Exception ex) {
ExceptionWebSocketHandlerDecorator.tryCloseWithError(this.wsSession, ex, logger);
}
}
Expand All @@ -123,7 +123,7 @@ private void handleBinaryMessage(javax.websocket.Session session, ByteBuffer pay
try {
this.handler.handleMessage(this.wsSession, binaryMessage);
}
catch (Throwable ex) {
catch (Exception ex) {
ExceptionWebSocketHandlerDecorator.tryCloseWithError(this.wsSession, ex, logger);
}
}
Expand All @@ -133,7 +133,7 @@ private void handlePongMessage(javax.websocket.Session session, ByteBuffer paylo
try {
this.handler.handleMessage(this.wsSession, pongMessage);
}
catch (Throwable ex) {
catch (Exception ex) {
ExceptionWebSocketHandlerDecorator.tryCloseWithError(this.wsSession, ex, logger);
}
}
Expand All @@ -144,7 +144,7 @@ public void onClose(javax.websocket.Session session, CloseReason reason) {
try {
this.handler.afterConnectionClosed(this.wsSession, closeStatus);
}
catch (Throwable ex) {
catch (Exception ex) {
if (logger.isWarnEnabled()) {
logger.warn("Unhandled on-close exception for " + this.wsSession, ex);
}
Expand All @@ -156,7 +156,7 @@ public void onError(javax.websocket.Session session, Throwable exception) {
try {
this.handler.handleTransportError(this.wsSession, exception);
}
catch (Throwable ex) {
catch (Exception ex) {
ExceptionWebSocketHandlerDecorator.tryCloseWithError(this.wsSession, ex, logger);
}
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -47,7 +47,7 @@ public void afterConnectionEstablished(WebSocketSession session) {
try {
getDelegate().afterConnectionEstablished(session);
}
catch (Throwable ex) {
catch (Exception ex) {
tryCloseWithError(session, ex, logger);
}
}
Expand All @@ -57,7 +57,7 @@ public void handleMessage(WebSocketSession session, WebSocketMessage<?> message)
try {
getDelegate().handleMessage(session, message);
}
catch (Throwable ex) {
catch (Exception ex) {
tryCloseWithError(session, ex, logger);
}
}
Expand All @@ -67,7 +67,7 @@ public void handleTransportError(WebSocketSession session, Throwable exception)
try {
getDelegate().handleTransportError(session, exception);
}
catch (Throwable ex) {
catch (Exception ex) {
tryCloseWithError(session, ex, logger);
}
}
Expand All @@ -77,7 +77,7 @@ public void afterConnectionClosed(WebSocketSession session, CloseStatus closeSta
try {
getDelegate().afterConnectionClosed(session, closeStatus);
}
catch (Throwable ex) {
catch (Exception ex) {
if (logger.isWarnEnabled()) {
logger.warn("Unhandled exception after connection closed for " + this, ex);
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -157,7 +157,7 @@ else if (websphereWsPresent) {
Class<?> clazz = ClassUtils.forName(className, AbstractHandshakeHandler.class.getClassLoader());
return (RequestUpgradeStrategy) ReflectionUtils.accessibleConstructor(clazz).newInstance();
}
catch (Throwable ex) {
catch (Exception ex) {
throw new IllegalStateException(
"Failed to instantiate RequestUpgradeStrategy: " + className, ex);
}
Expand Down
Expand Up @@ -77,7 +77,7 @@ public void applyAfterHandshake(
try {
interceptor.afterHandshake(request, response, this.wsHandler, failure);
}
catch (Throwable ex) {
catch (Exception ex) {
if (logger.isWarnEnabled()) {
logger.warn(interceptor + " threw exception in afterHandshake: " + ex);
}
Expand Down
Expand Up @@ -171,7 +171,7 @@ public void handleRequest(HttpServletRequest servletRequest, HttpServletResponse
catch (HandshakeFailureException ex) {
failure = ex;
}
catch (Throwable ex) {
catch (Exception ex) {
failure = new HandshakeFailureException("Uncaught failure for request " + request.getURI(), ex);
}
finally {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -244,7 +244,7 @@ private void handleOpenFrame() {
this.webSocketHandler.afterConnectionEstablished(this);
this.connectFuture.set(this);
}
catch (Throwable ex) {
catch (Exception ex) {
if (logger.isErrorEnabled()) {
logger.error("WebSocketHandler.afterConnectionEstablished threw exception in " + this, ex);
}
Expand Down Expand Up @@ -293,7 +293,7 @@ private void handleMessageFrame(SockJsFrame frame) {
try {
this.webSocketHandler.handleMessage(this, new TextMessage(message));
}
catch (Throwable ex) {
catch (Exception ex) {
logger.error("WebSocketHandler.handleMessage threw an exception on " + frame + " in " + this, ex);
}
}
Expand Down
Expand Up @@ -118,7 +118,7 @@ protected void connectInternal(final TransportRequest transportRequest, final We
getRestTemplate().execute(receiveUrl, HttpMethod.POST, requestCallback, responseExtractor);
requestCallback = requestCallbackAfterHandshake;
}
catch (Throwable ex) {
catch (Exception ex) {
if (!connectFuture.isDone()) {
connectFuture.setException(ex);
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -260,7 +260,7 @@ public final ListenableFuture<WebSocketSession> doHandshake(
ServerInfo serverInfo = getServerInfo(sockJsUrlInfo, getHttpRequestHeaders(headers));
createRequest(sockJsUrlInfo, headers, serverInfo).connect(handler, connectFuture);
}
catch (Throwable exception) {
catch (Exception exception) {
if (logger.isErrorEnabled()) {
logger.error("Initial SockJS \"Info\" request to server failed, url=" + url, exception);
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -133,7 +133,7 @@ public void handleRequest(HttpServletRequest servletRequest, HttpServletResponse
try {
this.sockJsService.handleRequest(request, response, getSockJsPath(servletRequest), this.webSocketHandler);
}
catch (Throwable ex) {
catch (Exception ex) {
throw new SockJsException("Uncaught failure in SockJS request, uri=" + request.getURI(), ex);
}
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -215,10 +215,10 @@ protected void handleRawWebSocketRequest(ServerHttpRequest request, ServerHttpRe
catch (HandshakeFailureException ex) {
failure = ex;
}
catch (Throwable ex) {
catch (Exception ex) {
failure = new HandshakeFailureException("Uncaught failure for request " + request.getURI(), ex);
}
finally {
finally {
if (failure != null) {
chain.applyAfterHandshake(request, response, failure);
throw failure;
Expand Down Expand Up @@ -316,7 +316,7 @@ else if (transportType.supportsCors()) {
catch (SockJsException ex) {
failure = ex;
}
catch (Throwable ex) {
catch (Exception ex) {
failure = new SockJsException("Uncaught failure for request " + request.getURI(), sessionId, ex);
}
finally {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -72,7 +72,7 @@ protected void handleRequestInternal(ServerHttpRequest request, ServerHttpRespon
}
return;
}
catch (Throwable ex) {
catch (Exception ex) {
logger.error("Failed to read message", ex);
handleReadError(response, "Failed to read message(s)", sockJsSession.getId());
return;
Expand Down
Expand Up @@ -124,7 +124,7 @@ public void handleRequest(ServerHttpRequest request, ServerHttpResponse response
wsHandler = new SockJsWebSocketHandler(getServiceConfig(), wsHandler, sockJsSession);
this.handshakeHandler.doHandshake(request, response, wsHandler, sockJsSession.getAttributes());
}
catch (Throwable ex) {
catch (Exception ex) {
sockJsSession.tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR);
throw new SockJsTransportFailureException("WebSocket handshake failure", wsSession.getId(), ex);
}
Expand Down
Expand Up @@ -324,7 +324,7 @@ protected void writeFrame(SockJsFrame frame) throws SockJsTransportFailureExcept
try {
writeFrameInternal(frame);
}
catch (Throwable ex) {
catch (Exception ex) {
logWriteFrameFailure(ex);
try {
// Force disconnect (so we won't try to send close frame)
Expand Down Expand Up @@ -388,7 +388,7 @@ public void delegateMessages(String... messages) throws SockJsMessageDeliveryExc
undelivered.remove(0);
}
}
catch (Throwable ex) {
catch (Exception ex) {
throw new SockJsMessageDeliveryException(this.id, undelivered, ex);
}
}
Expand Down
Expand Up @@ -166,7 +166,7 @@ public void initializeDelegateSession(WebSocketSession session) {
scheduleHeartbeat();
this.openFrameSent = true;
}
catch (Throwable ex) {
catch (Exception ex) {
tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR);
}
}
Expand All @@ -186,7 +186,7 @@ public void handleMessage(TextMessage message, WebSocketSession wsSession) throw
try {
messages = getSockJsServiceConfig().getMessageCodec().decode(payload);
}
catch (Throwable ex) {
catch (Exception ex) {
logger.error("Broken data received. Terminating WebSocket connection abruptly", ex);
tryCloseWithSockJsTransportError(ex, CloseStatus.BAD_DATA);
return;
Expand Down

0 comments on commit 526d89e

Please sign in to comment.