This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch 8.5.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/8.5.x by this push:
     new 28ae1a311a Improve error handling
28ae1a311a is described below

commit 28ae1a311a04fc66e521b5dba97dae523fd03ac6
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Wed Dec 6 15:06:09 2023 +0000

    Improve error handling
---
 webapps/docs/changelog.xml                         |  8 ++++++++
 .../classes/websocket/snake/SnakeAnnotation.java   | 23 ++++++++++++++--------
 2 files changed, 23 insertions(+), 8 deletions(-)

diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 6a577ef1cd..2b17983db3 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -136,6 +136,14 @@
       </scode>
     </changelog>
   </subsection>
+  <subsection name="Web Applications">
+    <changelog>
+      <fix>
+        Examples. Improve the error handling so snakes associated with a user
+        that drops from the network are removed from the game. (markt)
+      </fix>
+    </changelog>
+  </subsection>
   <subsection name="Other">
     <changelog>
       <update>
diff --git 
a/webapps/examples/WEB-INF/classes/websocket/snake/SnakeAnnotation.java 
b/webapps/examples/WEB-INF/classes/websocket/snake/SnakeAnnotation.java
index c030dbcabe..d3d9700021 100644
--- a/webapps/examples/WEB-INF/classes/websocket/snake/SnakeAnnotation.java
+++ b/webapps/examples/WEB-INF/classes/websocket/snake/SnakeAnnotation.java
@@ -17,7 +17,7 @@
 package websocket.snake;
 
 import java.awt.Color;
-import java.io.EOFException;
+import java.io.IOException;
 import java.util.Iterator;
 import java.util.Random;
 import java.util.concurrent.atomic.AtomicInteger;
@@ -115,19 +115,26 @@ public class SnakeAnnotation {
 
 
     @OnError
-    public void onError(Throwable t) throws Throwable {
-        // Most likely cause is a user closing their browser. Check to see if
-        // the root cause is EOF and if it is ignore it.
-        // Protect against infinite loops.
+    public void onError(Throwable t, Session session) throws Throwable {
+        /*
+         * Assume all errors are fatal. Close the session and remove the snake 
from the game.
+         */
+        session.close();
+        onClose();
+        /*
+         * Correct action depends on root cause. Protect against infinite 
loops while looking for root cause.
+         */
         int count = 0;
         Throwable root = t;
         while (root.getCause() != null && count < 20) {
             root = root.getCause();
             count ++;
         }
-        if (root instanceof EOFException) {
-            // Assume this is triggered by the user closing their browser and
-            // ignore it.
+        if (root instanceof IOException) {
+            /*
+             * User going away can present in different ways depending on 
their platform and exactly what went wrong.
+             * Assume that any IO issue is some form of the user going away 
and ignore it.
+             */
         } else {
             throw t;
         }


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to