Skip to content

Commit 0594917

Browse files
vaadin-botArtur-
andauthored
fix: log Vite build failure only once instead of on every request (#24085) (CP: 25.1) (#24088)
This PR cherry-picks changes from the original PR #24085 to branch 25.1. --- #### Original PR description > When the Vite dev server fails to start, the exception was re-thrown on every subsequent HTTP request, flooding the log with identical stack traces. Now the error is thrown once and subsequent requests gets the error in the browser instead. Co-authored-by: Artur Signell <artur@vaadin.com>
1 parent d934a56 commit 0594917

File tree

4 files changed

+32
-21
lines changed

4 files changed

+32
-21
lines changed

flow-build-tools/src/main/java/com/vaadin/flow/server/frontend/TaskRunDevBundleBuild.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,12 @@ private void runFrontendBuildTool(String toolName, String packageName,
208208
int errorCode = process.waitFor();
209209

210210
if (errorCode != 0) {
211-
logger.error("Command `{}` failed:\n{}", commandString,
212-
toolOutput);
213-
throw new ExecutionFailedException(
211+
ExecutionFailedException exception = new ExecutionFailedException(
214212
SharedUtil.capitalize(toolName)
215-
+ " build exited with a non zero status");
213+
+ " build exited with a non zero status:\n"
214+
+ toolOutput);
215+
logger.error("Command `{}` failed", commandString, exception);
216+
throw exception;
216217
} else {
217218
logger.info("Development frontend bundle built");
218219
}

vaadin-dev-server/src/main/java/com/vaadin/base/devserver/AbstractDevServerRunner.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import java.util.UUID;
3535
import java.util.concurrent.CompletableFuture;
3636
import java.util.concurrent.CompletionException;
37-
import java.util.concurrent.atomic.AtomicBoolean;
3837
import java.util.concurrent.atomic.AtomicReference;
3938
import java.util.function.BiConsumer;
4039
import java.util.regex.Pattern;
@@ -107,7 +106,7 @@ public abstract class AbstractDevServerRunner implements DevModeHandler {
107106
private final boolean reuseDevServer;
108107
private final File devServerPortFile;
109108

110-
private AtomicBoolean isDevServerFailedToStart = new AtomicBoolean();
109+
private AtomicReference<String> devServerFailure = new AtomicReference<>();
111110

112111
private final CompletableFuture<Void> devServerStartFuture;
113112

@@ -217,8 +216,11 @@ void doStartDevModeServer() throws ExecutionFailedException {
217216
Process process = doStartDevServer();
218217
devServerProcess.set(process);
219218
if (!isRunning()) {
220-
throw new IllegalStateException("Startup of " + getServerName()
221-
+ " failed. Output was:\n" + getFailedOutput());
219+
IllegalStateException exception = new IllegalStateException(
220+
"Startup of " + getServerName()
221+
+ " failed. Output was:\n" + getFailedOutput());
222+
getLogger().error("Dev server failed to start", exception);
223+
throw exception;
222224
}
223225

224226
long ms = (System.nanoTime() - start) / 1000000;
@@ -648,22 +650,30 @@ public HttpURLConnection prepareConnection(String path, String method)
648650
public boolean handleRequest(VaadinSession session, VaadinRequest request,
649651
VaadinResponse response) throws IOException {
650652
return handleRequestInternal(session, request, response,
651-
devServerStartFuture, isDevServerFailedToStart);
653+
devServerStartFuture, devServerFailure);
652654
}
653655

654656
static boolean handleRequestInternal(VaadinSession session,
655657
VaadinRequest request, VaadinResponse response,
656658
CompletableFuture<?> devServerStartFuture,
657-
AtomicBoolean isDevServerFailedToStart) throws IOException {
658-
if (devServerStartFuture.isDone()) {
659-
// The server has started, check for any exceptions in the startup
660-
// process
659+
AtomicReference<String> devServerFailure) throws IOException {
660+
if (devServerFailure.get() == null && devServerStartFuture.isDone()) {
661661
try {
662662
devServerStartFuture.getNow(null);
663663
} catch (CompletionException exception) {
664-
isDevServerFailedToStart.set(true);
665-
throw getCause(exception);
664+
RuntimeException cause = getCause(exception);
665+
devServerFailure.set("The Vite dev server failed to start: "
666+
+ cause.getMessage());
666667
}
668+
}
669+
String failureMessage = devServerFailure.get();
670+
if (failureMessage != null) {
671+
response.setContentType("text/html;charset=utf-8");
672+
response.setHeader("Cache-Control", "no-cache");
673+
response.getWriter().write("<pre>" + failureMessage + "</pre>");
674+
return true;
675+
}
676+
if (devServerStartFuture.isDone()) {
667677
if (request.getHeader("X-DevModePoll") != null) {
668678
// Avoid creating a UI that is thrown away for polling requests
669679
response.setContentType("text/html;charset=utf-8");
@@ -721,7 +731,7 @@ static boolean handleRequestInternal(VaadinSession session,
721731
public boolean serveDevModeRequest(HttpServletRequest request,
722732
HttpServletResponse response) throws IOException {
723733
// Do not serve requests if dev server starting or failed to start.
724-
if (isDevServerFailedToStart.get() || !devServerStartFuture.isDone()
734+
if (devServerFailure.get() != null || !devServerStartFuture.isDone()
725735
|| devServerStartFuture.isCompletedExceptionally()) {
726736
return false;
727737
}

vaadin-dev-server/src/main/java/com/vaadin/base/devserver/DevBundleBuildingHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import java.io.IOException;
2323
import java.net.HttpURLConnection;
2424
import java.util.concurrent.CompletableFuture;
25-
import java.util.concurrent.atomic.AtomicBoolean;
25+
import java.util.concurrent.atomic.AtomicReference;
2626

2727
import com.vaadin.flow.internal.DevModeHandler;
2828
import com.vaadin.flow.server.VaadinRequest;
@@ -45,6 +45,7 @@
4545
public final class DevBundleBuildingHandler implements DevModeHandler {
4646

4747
private final transient CompletableFuture<Void> buildCompletedFuture;
48+
private final AtomicReference<String> devServerFailure = new AtomicReference<>();
4849

4950
public DevBundleBuildingHandler(
5051
CompletableFuture<Void> buildCompletedFuture) {
@@ -91,7 +92,7 @@ public int getPort() {
9192
public boolean handleRequest(VaadinSession session, VaadinRequest request,
9293
VaadinResponse response) throws IOException {
9394
return AbstractDevServerRunner.handleRequestInternal(session, request,
94-
response, buildCompletedFuture, new AtomicBoolean());
95+
response, buildCompletedFuture, devServerFailure);
9596
}
9697

9798
/**

vaadin-dev-server/src/main/java/com/vaadin/base/devserver/startup/DevModeInitializer.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
import com.vaadin.flow.server.Mode;
6969
import com.vaadin.flow.server.VaadinContext;
7070
import com.vaadin.flow.server.VaadinServlet;
71-
import com.vaadin.flow.server.frontend.ExecutionFailedException;
7271
import com.vaadin.flow.server.frontend.NodeTasks;
7372
import com.vaadin.flow.server.frontend.Options;
7473
import com.vaadin.flow.server.frontend.installer.NodeInstaller;
@@ -389,8 +388,8 @@ static Set<File> getFrontendLocationsFromResourceProvider(
389388
private static void runNodeTasks(NodeTasks tasks) {
390389
try {
391390
tasks.execute();
392-
} catch (ExecutionFailedException exception) {
393-
log().debug(
391+
} catch (Exception exception) {
392+
log().error(
394393
"Could not initialize dev mode handler. One of the node tasks failed",
395394
exception);
396395
throw new CompletionException(exception);

0 commit comments

Comments
 (0)