Skip to content

Commit

Permalink
chore: More detailed logging for ignored handling (#20007) (#20028)
Browse files Browse the repository at this point in the history
Make log more detailed for
ignored invocation handle.
  • Loading branch information
caalador authored Sep 23, 2024
1 parent aa41079 commit 900389f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.vaadin.flow.internal.StateNode;
import com.vaadin.flow.internal.StateTree;
import com.vaadin.flow.internal.nodefeature.VirtualChildrenList;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.router.Router;
import com.vaadin.flow.server.Attributes;
import com.vaadin.flow.server.VaadinService;
Expand Down Expand Up @@ -647,4 +648,22 @@ public static Router getRouter(HasElement component) {
return router;
}

/**
* Walk up from given component until a Component with a Route annotation is
* found or empty if no Route is present in parents.
*
* @param component
* Component to find current route component for
* @return Optional containing Route component if found
*/
public static Optional<Component> getRouteComponent(Component component) {
if (component.getClass().isAnnotationPresent(Route.class)) {
return Optional.of(component);
}
if (component.getParent().isPresent()) {
return getRouteComponent(component.getParent().get());
}
return Optional.empty();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,21 @@
import java.util.List;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.ComponentUtil;
import com.vaadin.flow.component.PollEvent;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.dom.Element;
import com.vaadin.flow.internal.StateNode;
import com.vaadin.flow.internal.nodefeature.ElementData;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.shared.JsonConstants;

import elemental.json.JsonObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Abstract invocation handler implementation with common methods.
* <p>
Expand Down Expand Up @@ -60,21 +65,43 @@ public Optional<Runnable> handle(UI ui, JsonObject invocationJson) {
// ignore RPC requests from the client side for the nodes that are
// invisible, disabled or inert
if (node.isInactive()) {
getLogger().trace("Ignored RPC for invocation handler '{}' from "
+ "the client side for an inactive (disabled or invisible) node id='{}'",
getClass().getName(), node.getId());
logHandlingIgnoredMessage(node, "inactive (disabled or invisible)");
return Optional.empty();
} else if (!allowInert(ui, invocationJson) && node.isInert()) {
getLogger().trace(
"Ignored RPC for invocation handler '{}' from "
+ "the client side for an inert node id='{}'",
getClass().getName(), node.getId());
logHandlingIgnoredMessage(node, "inert");
return Optional.empty();
} else {
return handleNode(node, invocationJson);
}
}

private void logHandlingIgnoredMessage(StateNode node, String reason) {
StringBuilder targetInfo = new StringBuilder();
if (node != null && node.hasFeature(ElementData.class)) {
Element element = Element.get(node);
Optional<Component> component = element.getComponent();
targetInfo.append(" element with tag").append("'")
.append(element.getTag()).append("'");
if (component.isPresent()) {
targetInfo.append(" Component: ").append("'")
.append(component.get().getClass().getName())
.append("'");
Optional<Component> routeComponent = ComponentUtil
.getRouteComponent(component.get());
if (routeComponent.isPresent()) {
targetInfo.append(" Route: ").append("'")
.append(routeComponent.get().getClass()
.getAnnotation(Route.class).value())
.append("'");
}
}
}
getLogger().info(
"Ignored RPC for invocation handler '{}' from "
+ "the client side for an {} node id='{}'{}",
getClass().getName(), reason, node.getId(), targetInfo);
}

/**
* Checks whether a Poll RPC invocation is valid or not.
*
Expand Down

0 comments on commit 900389f

Please sign in to comment.