Skip to content

Commit

Permalink
#237 better error message when a template is not found through Direct…
Browse files Browse the repository at this point in the history
…oryCodeResolver
  • Loading branch information
casid committed Jul 9, 2023
1 parent 02f2bce commit 56c6976
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
18 changes: 18 additions & 0 deletions jte-runtime/src/main/java/gg/jte/CodeResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@ public interface CodeResolver {
*/
String resolve(String name);

/**
* Resolves the code of a template, which is required to exist.
* @param name The name of the template, e.g. <code>"tag/util/card.jte"</code>.
* @return The code of the resolved template, this is never <code>null</code>.
* @throws TemplateNotFoundException if no template with this name exists.
*
* Implementations that have better knowledge why the loading failed, are expected to
* override this method and provide information about the problem in the thrown exception message.
*/
default String resolveRequired(String name) throws TemplateNotFoundException {
String code = resolve(name);
if (code == null) {
throw new TemplateNotFoundException(name + " not found");
}

return code;
}

/**
* Resolves the last modification time of a template.
* @param name The name of the template, e.g. <code>"tag/util/card.jte"</code>.
Expand Down
8 changes: 4 additions & 4 deletions jte/src/main/java/gg/jte/compiler/TemplateCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -288,15 +288,15 @@ private CodeGenerator createCodeGenerator(ClassInfo classInfo, LinkedHashSet<Cla
}

private String resolveCode(String name, DebugInfo debugInfo) {
String code = codeResolver.resolve(name);
if (code == null) {
String message = name + " not found";
try {
return codeResolver.resolveRequired(name);
} catch (TemplateNotFoundException e) {
String message = e.getMessage();
if (debugInfo != null) {
message += ", referenced at " + debugInfo.name + ":" + debugInfo.line;
}
throw new TemplateNotFoundException(message);
}
return code;
}

@Override
Expand Down
15 changes: 13 additions & 2 deletions jte/src/main/java/gg/jte/resolve/DirectoryCodeResolver.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gg.jte.resolve;

import gg.jte.CodeResolver;
import gg.jte.TemplateNotFoundException;
import gg.jte.compiler.IoUtils;

import java.io.IOException;
Expand All @@ -24,10 +25,20 @@ public DirectoryCodeResolver(Path root) {
@Override
public String resolve(String name) {
try {
Path file = root.resolve(name);
return resolveRequired(name);
} catch (TemplateNotFoundException e) {
return null;
}
}

@Override
public String resolveRequired(String name) throws TemplateNotFoundException {
Path file = root.resolve(name);

try {
return Files.readString(file, StandardCharsets.UTF_8);
} catch (NoSuchFileException e) {
return null;
throw new TemplateNotFoundException(name + " not found (tried to load file at " + file.toAbsolutePath() + ")");
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.nio.file.Paths;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;

public class TemplateEngine_HasTemplate_DirectoryCodeResolverTest {

Expand All @@ -29,4 +30,13 @@ void templateExists() {
assertThat(codeResolver.exists("welcome.jte")).isTrue();
assertThat(templateEngine.hasTemplate("welcome.jte")).isTrue();
}

@Test
void resolveRequired() {
Throwable throwable = catchThrowable(() -> codeResolver.resolveRequired("does-not-exist.jte"));

assertThat(throwable).isInstanceOf(TemplateNotFoundException.class)
.hasMessageStartingWith("does-not-exist.jte not found (tried to load file at ")
.hasMessageEndingWith("does-not-exist.jte)");
}
}

0 comments on commit 56c6976

Please sign in to comment.