Skip to content

Commit b910db5

Browse files
manheychiumanheychiu1jasmith-hs
authored
Add a feature to output template error when the variable is undefined (#1174)
This PR adds a feature `OUTPUT_UNDEFINED_VARIABLES_ERROR`. When enabled, a template error (severity=WARNING) will be added when trying to resolve an undefined variable. --------- Co-authored-by: Manhey Chiu <mchiu@hubspot.com> Co-authored-by: Jack Smith <72623970+jasmith-hs@users.noreply.github.com>
1 parent 61452e4 commit b910db5

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ public class JinjavaInterpreter implements PyishSerializable {
7878

7979
public static final String IGNORED_OUTPUT_FROM_EXTENDS_NOTE =
8080
"ignored_output_from_extends";
81+
82+
public static final String OUTPUT_UNDEFINED_VARIABLES_ERROR =
83+
"OUTPUT_UNDEFINED_VARIABLES_ERROR";
8184
private final Multimap<String, BlockInfo> blocks = ArrayListMultimap.create();
8285
private final LinkedList<Node> extendParentRoots = new LinkedList<>();
8386
private final Map<String, RevertibleObject> revertibleObjects = new HashMap<>();
@@ -585,6 +588,28 @@ public Object retraceVariable(String variable, int lineNumber, int startPosition
585588
}
586589
}
587590
obj = var.resolve(obj);
591+
} else {
592+
if (
593+
getConfig()
594+
.getFeatures()
595+
.getActivationStrategy(OUTPUT_UNDEFINED_VARIABLES_ERROR)
596+
.isActive(context)
597+
) {
598+
addError(
599+
new TemplateError(
600+
ErrorType.WARNING,
601+
ErrorReason.UNKNOWN,
602+
ErrorItem.TOKEN,
603+
"Undefined variable: '" + variable + "'",
604+
null,
605+
lineNumber,
606+
startPosition,
607+
null,
608+
BasicTemplateErrorCategory.UNKNOWN,
609+
ImmutableMap.of("variable", variable)
610+
)
611+
);
612+
}
588613
}
589614
return obj;
590615
}

src/test/java/com/hubspot/jinjava/interpret/JinjavaInterpreterTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,4 +557,47 @@ public void itPreventsAccidentalExpressions() {
557557
JinjavaInterpreter.popCurrent();
558558
}
559559
}
560+
561+
@Test
562+
public void itOutputsUndefinedVariableError() {
563+
String template = "{% set foo=123 %}{{ foo }}{{ bar }}";
564+
565+
JinjavaInterpreter normalInterpreter = new JinjavaInterpreter(
566+
jinjava,
567+
jinjava.getGlobalContext(),
568+
JinjavaConfig.newBuilder().withExecutionMode(EagerExecutionMode.instance()).build()
569+
);
570+
JinjavaInterpreter outputtingErrorInterpreters = new JinjavaInterpreter(
571+
jinjava,
572+
jinjava.getGlobalContext(),
573+
JinjavaConfig
574+
.newBuilder()
575+
.withFeatureConfig(
576+
FeatureConfig
577+
.newBuilder()
578+
.add(
579+
JinjavaInterpreter.OUTPUT_UNDEFINED_VARIABLES_ERROR,
580+
FeatureStrategies.ACTIVE
581+
)
582+
.build()
583+
)
584+
.withExecutionMode(EagerExecutionMode.instance())
585+
.build()
586+
);
587+
588+
String normalRenderResult = normalInterpreter.render(template);
589+
String outputtingErrorRenderResult = outputtingErrorInterpreters.render(template);
590+
assertThat(normalRenderResult).isEqualTo("123");
591+
assertThat(outputtingErrorRenderResult).isEqualTo("123");
592+
assertThat(normalInterpreter.getErrors()).isEmpty();
593+
assertThat(outputtingErrorInterpreters.getErrors().size()).isEqualTo(1);
594+
assertThat(outputtingErrorInterpreters.getErrors().get(0).getMessage())
595+
.contains("Undefined variable: 'bar'");
596+
assertThat(outputtingErrorInterpreters.getErrors().get(0).getReason())
597+
.isEqualTo(ErrorReason.UNKNOWN);
598+
assertThat(outputtingErrorInterpreters.getErrors().get(0).getSeverity())
599+
.isEqualTo(ErrorType.WARNING);
600+
assertThat(outputtingErrorInterpreters.getErrors().get(0).getCategoryErrors())
601+
.isEqualTo(ImmutableMap.of("variable", "bar"));
602+
}
560603
}

0 commit comments

Comments
 (0)