Skip to content

Commit

Permalink
QuteErrorPageSetup: support templates that are not backed by a file
Browse files Browse the repository at this point in the history
- fixes #44412

(cherry picked from commit 8ce3dc5)
  • Loading branch information
mkouba authored and gsmet committed Nov 12, 2024
1 parent 53dc796 commit ec0a40d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.quarkus.qute.deployment;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.quarkus.arc.deployment.ValidationPhaseBuildItem.ValidationErrorBuildItem;
import io.quarkus.deployment.IsDevelopment;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.BuildSteps;
import io.quarkus.dev.console.DevConsoleManager;
import io.quarkus.qute.runtime.devmode.QuteErrorPageSetup;

@BuildSteps(onlyIf = IsDevelopment.class)
public class QuteDevModeProcessor {

@BuildStep
void collectGeneratedContents(List<TemplatePathBuildItem> templatePaths,
BuildProducer<ValidationErrorBuildItem> errors) {
Map<String, String> contents = new HashMap<>();
for (TemplatePathBuildItem template : templatePaths) {
if (!template.isFileBased()) {
contents.put(template.getPath(), template.getContent());
}
}
// Set the global that could be used at runtime when a qute error page is rendered
DevConsoleManager.setGlobal(QuteErrorPageSetup.GENERATED_CONTENTS, contents);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Files;
Expand All @@ -12,13 +13,15 @@
import java.util.Comparator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.stream.Collectors;

import org.jboss.logging.Logger;

import io.quarkus.dev.ErrorPageGenerators;
import io.quarkus.dev.console.DevConsoleManager;
import io.quarkus.dev.spi.HotReplacementContext;
import io.quarkus.dev.spi.HotReplacementSetup;
import io.quarkus.qute.Engine;
Expand All @@ -33,6 +36,8 @@ public class QuteErrorPageSetup implements HotReplacementSetup {

private static final Logger LOG = Logger.getLogger(QuteErrorPageSetup.class);

public static final String GENERATED_CONTENTS = "io.quarkus.qute.generatedContents";

private static final String TEMPLATE_EXCEPTION = "io.quarkus.qute.TemplateException";
private static final String ORIGIN = "io.quarkus.qute.TemplateNode$Origin";

Expand Down Expand Up @@ -139,6 +144,10 @@ String getProblemInfo(int index, Throwable problem, Template problemTemplate, Es
LOG.warn("Unable to read the template source: " + templateId, e);
}

if (sourceLines.isEmpty()) {
return Arrays.stream(messageLines).collect(Collectors.joining("<br>"));
}

List<Integer> realLines = new ArrayList<>();
boolean endLinesSkipped = false;
if (sourceLines.size() > 15) {
Expand Down Expand Up @@ -187,6 +196,14 @@ private BufferedReader getBufferedReader(String templateId) throws IOException {
}
}
}
// Source file not available - try to search the generated contents
Map<String, String> generatedContents = DevConsoleManager.getGlobal(GENERATED_CONTENTS);
if (generatedContents != null) {
String template = generatedContents.get(templateId);
if (template != null) {
return new BufferedReader(new StringReader(template));
}
}
throw new IllegalStateException("Template source not available");
}

Expand Down

0 comments on commit ec0a40d

Please sign in to comment.