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

QuteErrorPageSetup: support templates that are not backed by a file #44416

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
@@ -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