]> source.dussan.org Git - vaadin-framework.git/commitdiff
Send beforeClientResponse exceptions to an error handler (#14214)
authorLeif Åstrand <leif@vaadin.com>
Sun, 14 Jun 2015 12:46:14 +0000 (15:46 +0300)
committerTeppo Kurki <teppo.kurki@vaadin.com>
Wed, 17 Jun 2015 12:33:23 +0000 (12:33 +0000)
Change-Id: Ib0cd9a402bbef0c7adb65bd8298a71b5521edd7c

server/src/com/vaadin/server/LegacyCommunicationManager.java
server/src/com/vaadin/server/communication/UidlWriter.java
uitest/src/com/vaadin/tests/applicationservlet/SystemMessages.java
uitest/src/com/vaadin/tests/components/abstractcomponent/ResponseWritingErrorHandling.java [new file with mode: 0644]
uitest/src/com/vaadin/tests/components/abstractcomponent/ResponseWritingErrorHandlingTest.java [new file with mode: 0644]

index fda5ad444f80138a5fb7c6a523e11ebcdc625842..e982cdf10a5fe072dfb4d26337f2da3c255acf5b 100644 (file)
@@ -388,17 +388,15 @@ public class LegacyCommunicationManager implements Serializable {
     }
 
     /**
-     * Handles an exception that occurred when processing RPC calls or a file
-     * upload.
+     * Handles an exception related to a connector by invoking the appropriate
+     * error handler.
      * 
      * @deprecated As of 7.1. See #11411.
      * 
-     * @param ui
-     *            The UI where the exception occured
      * @param throwable
-     *            The exception
+     *            the exception to handle
      * @param connector
-     *            The Rpc target
+     *            the connector that the exception is related to
      */
     @Deprecated
     public void handleConnectorRelatedException(ClientConnector connector,
index 00a65d3877cc27a2f8b734f694b1ddd29c0b07e9..a4797e49aa6ca1d5303db6d84a03110bfca3bdb2 100644 (file)
@@ -109,7 +109,11 @@ public class UidlWriter implements Serializable {
                         .isClientSideInitialized(connector);
                 processedConnectors.add(connector);
 
-                connector.beforeClientResponse(!initialized);
+                try {
+                    connector.beforeClientResponse(!initialized);
+                } catch (RuntimeException e) {
+                    manager.handleConnectorRelatedException(connector, e);
+                }
             }
         }
 
index 00547aa2d2689485014add3e6542da94ebbba40d..9912e660ef4fe83e45841d98ffd525d00dc17098 100644 (file)
@@ -14,13 +14,15 @@ import com.vaadin.ui.Button.ClickEvent;
 import com.vaadin.ui.Button.ClickListener;
 import com.vaadin.ui.NativeSelect;
 
+import elemental.json.JsonObject;
+
 public class SystemMessages extends AbstractTestUI {
 
     public class MyButton extends Button {
         private boolean fail = false;
 
         @Override
-        public void beforeClientResponse(boolean initial) {
+        public JsonObject encodeState() {
             // Set the error message to contain the current locale.
             VaadinService.getCurrentRequest().setAttribute(
                     ApplicationRunnerServlet.CUSTOM_SYSTEM_MESSAGES_PROPERTY,
@@ -30,9 +32,10 @@ public class SystemMessages extends AbstractTestUI {
                             return "MessagesInfo locale: " + getLocale();
                         }
                     });
-            super.beforeClientResponse(initial);
             if (fail) {
                 throw new RuntimeException("Failed on purpose");
+            } else {
+                return super.encodeState();
             }
         }
     }
diff --git a/uitest/src/com/vaadin/tests/components/abstractcomponent/ResponseWritingErrorHandling.java b/uitest/src/com/vaadin/tests/components/abstractcomponent/ResponseWritingErrorHandling.java
new file mode 100644 (file)
index 0000000..23be0f6
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.tests.components.abstractcomponent;
+
+import com.vaadin.server.ErrorHandler;
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.tests.components.AbstractTestUIWithLog;
+import com.vaadin.ui.Button;
+
+public class ResponseWritingErrorHandling extends AbstractTestUIWithLog {
+
+    @Override
+    protected void setup(VaadinRequest request) {
+        ErrorHandler errorHandler = new ErrorHandler() {
+            @Override
+            public void error(com.vaadin.server.ErrorEvent event) {
+                String message = event.getThrowable().getMessage();
+                log(message);
+            }
+        };
+
+        Button button = new Button("Throw in beforeClientResponse") {
+            private boolean throwInBeforeClientResponse = false;
+            {
+                addClickListener(new ClickListener() {
+                    @Override
+                    public void buttonClick(ClickEvent event) {
+                        throwInBeforeClientResponse = true;
+                        // Make sure beforeClientResponse is called
+                        markAsDirty();
+                    }
+                });
+            }
+
+            @Override
+            public void beforeClientResponse(boolean initial) {
+                if (throwInBeforeClientResponse) {
+                    throwInBeforeClientResponse = false;
+                    throw new RuntimeException("Button.beforeClientResponse");
+                }
+            }
+        };
+        button.setErrorHandler(errorHandler);
+
+        addComponent(button);
+    }
+
+}
diff --git a/uitest/src/com/vaadin/tests/components/abstractcomponent/ResponseWritingErrorHandlingTest.java b/uitest/src/com/vaadin/tests/components/abstractcomponent/ResponseWritingErrorHandlingTest.java
new file mode 100644 (file)
index 0000000..7b939a5
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.tests.components.abstractcomponent;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.vaadin.testbench.elements.ButtonElement;
+import com.vaadin.tests.tb3.SingleBrowserTest;
+
+public class ResponseWritingErrorHandlingTest extends SingleBrowserTest {
+
+    @Test
+    public void testExceptionInBeforeClientResponse() {
+        openTestURL();
+
+        $(ButtonElement.class).first().click();
+
+        Assert.assertEquals("Message should be logged by error handler",
+                "1. Button.beforeClientResponse", getLogRow(0));
+    }
+}