From 024a2ab52e1ab5ac039c78f86c9c6fbf3a70f131 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Sun, 14 Jun 2015 15:46:14 +0300 Subject: [PATCH] Send beforeClientResponse exceptions to an error handler (#14214) Change-Id: Ib0cd9a402bbef0c7adb65bd8298a71b5521edd7c --- .../server/LegacyCommunicationManager.java | 10 ++- .../server/communication/UidlWriter.java | 6 +- .../applicationservlet/SystemMessages.java | 7 ++- .../ResponseWritingErrorHandling.java | 61 +++++++++++++++++++ .../ResponseWritingErrorHandlingTest.java | 35 +++++++++++ 5 files changed, 110 insertions(+), 9 deletions(-) create mode 100644 uitest/src/com/vaadin/tests/components/abstractcomponent/ResponseWritingErrorHandling.java create mode 100644 uitest/src/com/vaadin/tests/components/abstractcomponent/ResponseWritingErrorHandlingTest.java diff --git a/server/src/com/vaadin/server/LegacyCommunicationManager.java b/server/src/com/vaadin/server/LegacyCommunicationManager.java index fda5ad444f..e982cdf10a 100644 --- a/server/src/com/vaadin/server/LegacyCommunicationManager.java +++ b/server/src/com/vaadin/server/LegacyCommunicationManager.java @@ -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, diff --git a/server/src/com/vaadin/server/communication/UidlWriter.java b/server/src/com/vaadin/server/communication/UidlWriter.java index 00a65d3877..a4797e49aa 100644 --- a/server/src/com/vaadin/server/communication/UidlWriter.java +++ b/server/src/com/vaadin/server/communication/UidlWriter.java @@ -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); + } } } diff --git a/uitest/src/com/vaadin/tests/applicationservlet/SystemMessages.java b/uitest/src/com/vaadin/tests/applicationservlet/SystemMessages.java index 00547aa2d2..9912e660ef 100644 --- a/uitest/src/com/vaadin/tests/applicationservlet/SystemMessages.java +++ b/uitest/src/com/vaadin/tests/applicationservlet/SystemMessages.java @@ -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 index 0000000000..23be0f62b4 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/abstractcomponent/ResponseWritingErrorHandling.java @@ -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 index 0000000000..7b939a5627 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/abstractcomponent/ResponseWritingErrorHandlingTest.java @@ -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)); + } +} -- 2.39.5