Skip to content

Commit

Permalink
fix: workaround for possible bug in route registry (like #9360).
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Anisimov committed Mar 16, 2021
1 parent 7d477e3 commit a0a3bbe
Showing 1 changed file with 50 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import com.vaadin.flow.component.Component;
import com.vaadin.flow.router.InternalServerError;
import com.vaadin.flow.router.NotFoundException;
import com.vaadin.flow.router.RouteAliasData;
import com.vaadin.flow.router.RouteBaseData;
import com.vaadin.flow.router.RouteConfiguration;
import com.vaadin.flow.router.RouteData;
import com.vaadin.flow.router.RouteNotFoundError;
Expand Down Expand Up @@ -118,27 +120,57 @@ private void subscribeToChanges(RouteRegistry routeRegistry) {
private void applyChange(RoutesChangedEvent event) {
final RouteConfiguration routeConfiguration = RouteConfiguration
.forRegistry(this);
event.getRemovedRoutes()
.forEach(routeBaseData -> routeConfiguration.removeRoute(
routeBaseData.getUrl(),
routeBaseData.getNavigationTarget()));
event.getAddedRoutes()
.forEach(routeBaseData -> routeConfiguration.setRoute(
routeBaseData.getUrl(),
routeBaseData.getNavigationTarget(),
routeBaseData.getParentLayouts()));
Exception caught = null;
for (RouteBaseData<?> data : event.getRemovedRoutes()) {
caught = modifyRoute(() -> routeConfiguration
.removeRoute(data.getUrl(), data.getNavigationTarget()),
caught != null);
}
for (RouteBaseData<?> data : event.getAddedRoutes()) {
caught = modifyRoute(() -> routeConfiguration.setRoute(
data.getUrl(), data.getNavigationTarget(),
data.getParentLayouts()), caught != null);
}
handleCaughtException(caught);
}

private void setRoutes(List<RouteData> routes) {
routes.forEach(routeData -> {
setRoute(routeData.getUrl(), routeData.getNavigationTarget(),
routeData.getParentLayouts());
routeData.getRouteAliases()
.forEach(routeAliasData -> setRoute(
routeAliasData.getUrl(),
routeAliasData.getNavigationTarget(),
routeAliasData.getParentLayouts()));
});
Exception caught = null;
for (RouteData data : routes) {
caught = modifyRoute(() -> setRoute(data.getUrl(),
data.getNavigationTarget(), data.getParentLayouts()),
caught != null);
for (RouteAliasData alias : data.getRouteAliases()) {
caught = modifyRoute(() -> setRoute(alias.getUrl(),
alias.getNavigationTarget(),
alias.getParentLayouts()), caught != null);
}
}
handleCaughtException(caught);
}

private void handleCaughtException(Exception exception) {
if (exception instanceof RuntimeException) {
throw (RuntimeException) exception;
} else if (exception != null) {
// should not be possible
throw new IllegalStateException(exception);
}
}

private Exception modifyRoute(Runnable runnable, boolean logException) {
try {
runnable.run();
return null;
} catch (Exception exception) {
if (logException) {
LoggerFactory.getLogger(OSGiRouteRegistry.class)
.error("Route remove exception thrown", exception);
return null;
} else {
return exception;
}
}
}
}

Expand Down

0 comments on commit a0a3bbe

Please sign in to comment.