This is an automated email from the ASF dual-hosted git repository.
Aias00 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new decaaaccc4 fix: add try-catch to webSocket onMessage (#6961)
decaaaccc4 is described below
commit decaaaccc41a2b434dd92a0f207c00f9a3ac0000
Author: hengyuss <[email protected]>
AuthorDate: Sat Sep 5 10:19:26 2026 +0800
fix: add try-catch to webSocket onMessage (#6961)
Co-authored-by: aias00 <[email protected]>
---
.../websocket/client/ShenyuWebsocketClient.java | 28 ++++++++++++----------
.../client/ShenyuWebsocketClientTest.java | 26 ++++++++++++++++++++
2 files changed, 42 insertions(+), 12 deletions(-)
diff --git
a/shenyu-sync-data-center/shenyu-sync-data-websocket/src/main/java/org/apache/shenyu/plugin/sync/data/websocket/client/ShenyuWebsocketClient.java
b/shenyu-sync-data-center/shenyu-sync-data-websocket/src/main/java/org/apache/shenyu/plugin/sync/data/websocket/client/ShenyuWebsocketClient.java
index 76917229d0..20732a78fe 100644
---
a/shenyu-sync-data-center/shenyu-sync-data-websocket/src/main/java/org/apache/shenyu/plugin/sync/data/websocket/client/ShenyuWebsocketClient.java
+++
b/shenyu-sync-data-center/shenyu-sync-data-websocket/src/main/java/org/apache/shenyu/plugin/sync/data/websocket/client/ShenyuWebsocketClient.java
@@ -228,19 +228,23 @@ public final class ShenyuWebsocketClient extends
WebSocketClient {
if (LOG.isDebugEnabled()) {
LOG.debug("onMessage server[{}] result({})",
this.getURI().toString(), result);
}
-
- Map<String, Object> jsonToMap = JsonUtils.jsonToMap(result);
- Object eventType = jsonToMap.get(RunningModeConstants.EVENT_TYPE);
- if (Objects.equals(DataEventTypeEnum.RUNNING_MODE.name(), eventType)) {
- LOG.info("server[{}] handle running mode result({})",
this.getURI().toString(), result);
- this.runningMode =
String.valueOf(jsonToMap.get(RunningModeConstants.RUNNING_MODE));
- if (Objects.equals(RunningModeEnum.STANDALONE.name(),
runningMode)) {
- return;
+
+ try {
+ Map<String, Object> jsonToMap = JsonUtils.jsonToMap(result);
+ Object eventType = jsonToMap.get(RunningModeConstants.EVENT_TYPE);
+ if (Objects.equals(DataEventTypeEnum.RUNNING_MODE.name(),
eventType)) {
+ LOG.info("server[{}] handle running mode result({})",
this.getURI().toString(), result);
+ this.runningMode =
String.valueOf(jsonToMap.get(RunningModeConstants.RUNNING_MODE));
+ if (Objects.equals(RunningModeEnum.STANDALONE.name(),
runningMode)) {
+ return;
+ }
+ this.masterUrl =
String.valueOf(jsonToMap.get(RunningModeConstants.MASTER_URL));
+ this.isConnectedToMaster =
Boolean.TRUE.equals(jsonToMap.get(RunningModeConstants.IS_MASTER));
+ } else {
+ handleResult(result);
}
- this.masterUrl =
String.valueOf(jsonToMap.get(RunningModeConstants.MASTER_URL));
- this.isConnectedToMaster =
Boolean.TRUE.equals(jsonToMap.get(RunningModeConstants.IS_MASTER));
- } else {
- handleResult(result);
+ } catch (RuntimeException ex) {
+ LOG.warn("Failed to handle websocket message from server[{}], the
message will be ignored", this.getURI(), ex);
}
}
diff --git
a/shenyu-sync-data-center/shenyu-sync-data-websocket/src/test/java/org/apache/shenyu/plugin/sync/data/websocket/client/ShenyuWebsocketClientTest.java
b/shenyu-sync-data-center/shenyu-sync-data-websocket/src/test/java/org/apache/shenyu/plugin/sync/data/websocket/client/ShenyuWebsocketClientTest.java
index db730837c0..82abc3b8e7 100644
---
a/shenyu-sync-data-center/shenyu-sync-data-websocket/src/test/java/org/apache/shenyu/plugin/sync/data/websocket/client/ShenyuWebsocketClientTest.java
+++
b/shenyu-sync-data-center/shenyu-sync-data-websocket/src/test/java/org/apache/shenyu/plugin/sync/data/websocket/client/ShenyuWebsocketClientTest.java
@@ -120,6 +120,32 @@ public class ShenyuWebsocketClientTest {
verify(pluginDataSubscriber).onSubscribe(any());
}
+ @Test
+ public void testOnMessageShouldIgnoreMalformedJsonAndHandleNextMessage() {
+ Assertions.assertDoesNotThrow(() ->
shenyuWebsocketClient.onMessage("{invalid json"));
+
+ doNothing().when(pluginDataSubscriber).onSubscribe(any());
+ String json = GsonUtils.getInstance().toJson(websocketData);
+ shenyuWebsocketClient.onMessage(json);
+ verify(pluginDataSubscriber).onSubscribe(any());
+ }
+
+ @Test
+ public void testOnMessageShouldIgnoreUnknownGroupType() {
+ websocketData.setGroupType("UNKNOWN_GROUP");
+ String json = GsonUtils.getInstance().toJson(websocketData);
+
+ Assertions.assertDoesNotThrow(() ->
shenyuWebsocketClient.onMessage(json));
+ }
+
+ @Test
+ public void testOnMessageShouldIgnoreUnknownEventType() {
+ websocketData.setEventType("UNKNOWN_EVENT");
+ String json = GsonUtils.getInstance().toJson(websocketData);
+
+ Assertions.assertDoesNotThrow(() ->
shenyuWebsocketClient.onMessage(json));
+ }
+
@Test
public void testOnClose() {
shenyuWebsocketClient = spy(shenyuWebsocketClient);