From ff41bf946b791470c5d4fe98c7cb9174785eac3d Mon Sep 17 00:00:00 2001 From: Teppo Kurki Date: Mon, 9 Dec 2024 17:48:32 +0200 Subject: [PATCH] fix: catch exceptions from detach calls --- .../java/com/vaadin/flow/component/ComponentUtil.java | 8 +++++++- .../com/vaadin/flow/component/DetachNotifier.java | 11 ++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/flow-server/src/main/java/com/vaadin/flow/component/ComponentUtil.java b/flow-server/src/main/java/com/vaadin/flow/component/ComponentUtil.java index c6f13ddb902..711ebb73c51 100644 --- a/flow-server/src/main/java/com/vaadin/flow/component/ComponentUtil.java +++ b/flow-server/src/main/java/com/vaadin/flow/component/ComponentUtil.java @@ -48,6 +48,7 @@ import com.vaadin.flow.router.Route; import com.vaadin.flow.router.Router; import com.vaadin.flow.server.Attributes; +import com.vaadin.flow.server.ErrorEvent; import com.vaadin.flow.server.VaadinService; import com.vaadin.flow.server.VaadinSession; import com.vaadin.flow.shared.Registration; @@ -338,7 +339,12 @@ public static void onComponentDetach(Component component) { } DetachEvent detachEvent = new DetachEvent(component); - component.onDetach(detachEvent); + try { + component.onDetach(detachEvent); + } catch (RuntimeException e) { + VaadinSession.getCurrent().getErrorHandler() + .error(new ErrorEvent(e)); + } fireEvent(component, detachEvent); // inform component about onEnabledState if parent and child states diff --git a/flow-server/src/main/java/com/vaadin/flow/component/DetachNotifier.java b/flow-server/src/main/java/com/vaadin/flow/component/DetachNotifier.java index 28f0259dad1..238fcce09d1 100644 --- a/flow-server/src/main/java/com/vaadin/flow/component/DetachNotifier.java +++ b/flow-server/src/main/java/com/vaadin/flow/component/DetachNotifier.java @@ -17,6 +17,8 @@ import java.io.Serializable; +import com.vaadin.flow.server.ErrorEvent; +import com.vaadin.flow.server.VaadinSession; import com.vaadin.flow.shared.Registration; /** @@ -38,7 +40,14 @@ default Registration addDetachListener( ComponentEventListener listener) { if (this instanceof Component) { return ComponentUtil.addListener((Component) this, - DetachEvent.class, listener); + DetachEvent.class, event -> { + try { + listener.onComponentEvent(event); + } catch (RuntimeException e) { + VaadinSession.getCurrent().getErrorHandler() + .error(new ErrorEvent(e)); + } + }); } else { throw new IllegalStateException(String.format( "The class '%s' doesn't extend '%s'. "