Skip to content
Open
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
Expand Up @@ -100,10 +100,11 @@ public void apply(final Context context, final Writer writer) throws IOException
} catch (Exception ex) {
String evidence = toString();
String reason = ex.toString();
String message = filename + ":" + line + ":" + column + ": " + reason + "\n";
int errorColumn = Math.max(1, column);
String message = filename + ":" + line + ":" + errorColumn + ": " + reason + "\n";
message += " " + join(split(evidence, "\n"), "\n ");
HandlebarsError error =
new HandlebarsError(filename, line, column, reason, evidence, message);
new HandlebarsError(filename, line, errorColumn, reason, evidence, message);
HandlebarsException hex = new HandlebarsException(error, ex);
// Override the stack-trace
hex.setStackTrace(ex.getStackTrace());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
package com.github.jknack.handlebars.internal;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.FilterWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
Expand All @@ -17,6 +22,7 @@

import com.github.jknack.handlebars.AbstractTest;
import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.HandlebarsException;
import com.github.jknack.handlebars.TagType;
import com.github.jknack.handlebars.Template;

Expand All @@ -28,6 +34,25 @@
*/
public class TemplateTest extends AbstractTest {

@Test
public void preservesWriterFailureAtStartOfLine() throws IOException {
IOException failure = new IOException("Unable to write template output");
Writer writer =
new FilterWriter(new StringWriter()) {
@Override
public void write(String text, int offset, int length) throws IOException {
throw failure;
}
};
Template template = newHandlebars().compileInline("Hello {{user}}.");

HandlebarsException error =
assertThrows(HandlebarsException.class, () -> template.apply("", writer));

assertSame(failure, error.getCause());
assertEquals(1, error.getError().column);
}

@Test
public void simpleVarTests() throws IOException {
assertVariables("{{v1}}", Arrays.asList("v1"));
Expand Down
Loading