GH-3421: Resolve throws Exception; in the API

Fixes https://github.com/spring-projects/spring-integration/issues/3421

Remove `throws Exception;` from production code to honor
the rule `Generic exceptions should never be thrown` which is enabled on SonarQube

* Rework affected usages to `try..catch` with throwing respective runtime exception
or just logging
* Some other refactoring for the affected classes
This commit is contained in:
Artem Bilan
2022-10-14 16:47:00 -04:00
committed by Gary Russell
parent a08713fe87
commit cfeaecaae8
4 changed files with 72 additions and 65 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2019 the original author or authors.
* Copyright 2014-2022 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.
@@ -42,25 +42,20 @@ public interface WebSocketListener extends SubProtocolCapable {
* Handle the received {@link WebSocketMessage}.
* @param session the WebSocket session
* @param message the WebSocket message
* @throws Exception the 'onMessage' Exception
*/
void onMessage(WebSocketSession session, WebSocketMessage<?> message)
throws Exception; // NOSONAR Remove in 5.2
void onMessage(WebSocketSession session, WebSocketMessage<?> message);
/**
* Invoked after a {@link WebSocketSession} has started.
* @param session the WebSocket session
* @throws Exception the 'afterSessionStarted' Exception
*/
void afterSessionStarted(WebSocketSession session) throws Exception; // NOSONAR Remove in 5.2
void afterSessionStarted(WebSocketSession session);
/**
* Invoked after a {@link WebSocketSession} has ended.
* @param session the WebSocket session
* @param closeStatus the reason why the session was closed
* @throws Exception the 'afterSessionEnded' Exception
*/
void afterSessionEnded(WebSocketSession session, CloseStatus closeStatus)
throws Exception; // NOSONAR Remove in 5.2
void afterSessionEnded(WebSocketSession session, CloseStatus closeStatus);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2021 the original author or authors.
* Copyright 2014-2022 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.
@@ -224,39 +224,54 @@ public class WebSocketInboundChannelAdapter extends MessageProducerSupport
}
@Override
public void afterSessionStarted(WebSocketSession session) throws Exception { // NOSONAR Thrown from the delegate
public void afterSessionStarted(WebSocketSession session) {
if (isActive()) {
SubProtocolHandler protocolHandler = this.subProtocolHandlerRegistry.findProtocolHandler(session);
protocolHandler.afterSessionStarted(session, this.subProtocolHandlerChannel);
if (!this.server && protocolHandler instanceof StompSubProtocolHandler) {
StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.CONNECT);
accessor.setSessionId(session.getId());
accessor.setLeaveMutable(true);
accessor.setAcceptVersion("1.1,1.2");
try {
SubProtocolHandler protocolHandler = this.subProtocolHandlerRegistry.findProtocolHandler(session);
protocolHandler.afterSessionStarted(session, this.subProtocolHandlerChannel);
if (!this.server && protocolHandler instanceof StompSubProtocolHandler) {
StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.CONNECT);
accessor.setSessionId(session.getId());
accessor.setLeaveMutable(true);
accessor.setAcceptVersion("1.1,1.2");
Message<?> connectMessage = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());
protocolHandler.handleMessageToClient(session, connectMessage);
Message<?> connectMessage =
MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());
protocolHandler.handleMessageToClient(session, connectMessage);
}
}
catch (Exception ex) {
logger.error(ex, () -> "WebSocketHandler.afterConnectionEstablished threw exception in " + this);
}
}
}
@Override
public void afterSessionEnded(WebSocketSession session, CloseStatus closeStatus)
throws Exception { // NOSONAR Thrown from the delegate
public void afterSessionEnded(WebSocketSession session, CloseStatus closeStatus) {
if (isActive()) {
this.subProtocolHandlerRegistry.findProtocolHandler(session)
.afterSessionEnded(session, closeStatus, this.subProtocolHandlerChannel);
try {
this.subProtocolHandlerRegistry.findProtocolHandler(session)
.afterSessionEnded(session, closeStatus, this.subProtocolHandlerChannel);
}
catch (Exception ex) {
logger.warn(ex, () -> "Unhandled exception after connection closed for " + this);
}
}
}
@Override
public void onMessage(WebSocketSession session, WebSocketMessage<?> webSocketMessage)
throws Exception { // NOSONAR Thrown from the delegate
public void onMessage(WebSocketSession session, WebSocketMessage<?> webSocketMessage) {
if (isActive()) {
this.subProtocolHandlerRegistry.findProtocolHandler(session)
.handleMessageFromClient(session, webSocketMessage, this.subProtocolHandlerChannel);
try {
this.subProtocolHandlerRegistry.findProtocolHandler(session)
.handleMessageFromClient(session, webSocketMessage, this.subProtocolHandlerChannel);
}
catch (Exception ex) {
logger.error(ex,
() -> "SubProtocolHandler.handleMessageFromClient threw an exception on message: " +
webSocketMessage + " in " + this);
}
}
}