Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make route config errors more prominent #20847

Merged
merged 5 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,15 @@
import java.io.EOFException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;

import com.vaadin.experimental.FeatureFlags;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.HasElement;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.di.Instantiator;
import com.vaadin.flow.router.BeforeEnterEvent;
import com.vaadin.flow.router.ErrorParameter;
import com.vaadin.flow.router.HasErrorParameter;
import com.vaadin.flow.router.InvalidLocationException;
import com.vaadin.flow.router.NavigationEvent;
import com.vaadin.flow.router.NavigationTrigger;
import com.vaadin.flow.router.RouteConfiguration;
import com.vaadin.flow.router.RouterLayout;
import com.vaadin.flow.router.internal.ErrorTargetEntry;
import com.vaadin.flow.router.internal.RouteUtil;
import com.vaadin.flow.server.startup.ApplicationRouteRegistry;

/**
* The default implementation of {@link ErrorHandler}.
Expand Down Expand Up @@ -74,9 +56,18 @@
public class DefaultErrorHandler implements ErrorHandler {

private final Set<String> ignoredExceptions;
private final Set<String> routeConfigurationExceptions;

protected DefaultErrorHandler(Set<String> ignoredExceptions) {
this.ignoredExceptions = Set.copyOf(ignoredExceptions);
this.routeConfigurationExceptions = new HashSet<>();
}

protected DefaultErrorHandler(Set<String> ignoredExceptions,
Set<String> routeConfigurationExceptions) {
this.ignoredExceptions = Set.copyOf(ignoredExceptions);
this.routeConfigurationExceptions = Set
.copyOf(routeConfigurationExceptions);
}

public DefaultErrorHandler() {
Expand All @@ -85,6 +76,10 @@ public DefaultErrorHandler() {
EOFException.class.getName(),
"org.eclipse.jetty.io.EofException",
"org.apache.catalina.connector.ClientAbortException");
this.routeConfigurationExceptions = Set.of(
AmbiguousRouteConfigurationException.class.getName(),
InvalidRouteConfigurationException.class.getName(),
InvalidRouteLayoutConfigurationException.class.getName());
}

@Override
Expand All @@ -101,8 +96,14 @@ public void error(ErrorEvent event) {
getLogger().warn(marker, "", throwable);
}
} else {
// print the error on console
getLogger().error("", throwable);
if (routeConfigurationExceptions
.contains(throwable.getClass().getName())) {
getLogger().error("Route configuration error found:");
getLogger().error(throwable.getMessage());
} else {
// print the error on console
getLogger().error("", throwable);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@
*/
package com.vaadin.flow.server.startup;

import com.vaadin.flow.server.InvalidRouteConfigurationException;
import com.vaadin.flow.server.InvalidRouteLayoutConfigurationException;
import com.vaadin.flow.server.VaadinServletContext;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import java.util.Set;

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

/**
* Allows a library/runtime to be notified of a web application's startup phase
* and perform any required programmatic registration of servlets, filters, and
Expand All @@ -41,8 +46,17 @@ default void process(Set<Class<?>> classSet, ServletContext context)
try {
initialize(classSet, new VaadinServletContext(context));
} catch (VaadinInitializerException e) {
if (e.getCause() instanceof InvalidRouteConfigurationException || e
.getCause() instanceof InvalidRouteLayoutConfigurationException) {
getLogger().error("Route configuration error found:");
getLogger().error(e.getCause().getMessage());
}
throw new ServletException(e);
}
}

private static Logger getLogger() {
return LoggerFactory
.getLogger(VaadinServletContextStartupInitializer.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,8 @@ public void failFastContextInitialized(ServletContextEvent event) {

} catch (InvalidRouteConfigurationException
| InvalidRouteLayoutConfigurationException e) {
getLogger().error("Route configuration error found:");
getLogger().error(e.getMessage());
throw new IllegalStateException(e);
}
} else {
Expand Down
Loading