Skip to content

Commit

Permalink
Merge pull request #157 from HubSpot/add-scope-level-to-errors
Browse files Browse the repository at this point in the history
add scope depth to errors
  • Loading branch information
boulter authored Nov 11, 2017
2 parents b4df68c + 64641ce commit 03c35d2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public class JinjavaInterpreter {
private final Random random;

private int lineNumber = -1;
private int scopeDepth = 1;
private final List<TemplateError> errors = new LinkedList<>();

public JinjavaInterpreter(Jinjava application, Context context, JinjavaConfig renderConfig) {
Expand All @@ -87,6 +88,7 @@ public JinjavaInterpreter(Jinjava application, Context context, JinjavaConfig re

public JinjavaInterpreter(JinjavaInterpreter orig) {
this(orig.application, new Context(orig.context), orig.config);
scopeDepth = orig.getScopeDepth() + 1;
}

/**
Expand Down Expand Up @@ -124,11 +126,13 @@ public InterpreterScopeClosable enterScope() {

public InterpreterScopeClosable enterScope(Map<Context.Library, Set<String>> disabled) {
context = new Context(context, null, disabled);
scopeDepth++;
return new InterpreterScopeClosable();
}

public void leaveScope() {
Context parent = context.getParent();
scopeDepth--;
if (parent != null) {
parent.addDependencies(context.getDependencies());
context = parent;
Expand Down Expand Up @@ -424,7 +428,11 @@ public int getLineNumber() {
}

public void addError(TemplateError templateError) {
this.errors.add(templateError);
this.errors.add(templateError.withScopeDepth(scopeDepth));
}

public int getScopeDepth() {
return scopeDepth;
}

public List<TemplateError> getErrors() {
Expand Down
39 changes: 38 additions & 1 deletion src/main/java/com/hubspot/jinjava/interpret/TemplateError.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,14 @@ public enum ErrorItem {
private final TemplateErrorCategory category;
private final Map<String, String> categoryErrors;

private int scopeDepth = 1;

private final Exception exception;

public TemplateError withScopeDepth(int scopeDepth) {
return new TemplateError(getSeverity(), getReason(), getItem(), getMessage(), getFieldName(), getLineno(), getStartPosition(), getException(), getCategory(), getCategoryErrors(), scopeDepth);
}

public static TemplateError fromSyntaxError(InterpretException ex) {
return new TemplateError(ErrorType.FATAL, ErrorReason.SYNTAX_ERROR, ErrorItem.OTHER, ExceptionUtils.getMessage(ex), null, ex.getLineNumber(), ex.getStartPosition(), ex);
}
Expand Down Expand Up @@ -144,6 +150,32 @@ public TemplateError(ErrorType severity,
this.categoryErrors = null;
}


public TemplateError(ErrorType severity,
ErrorReason reason,
ErrorItem item,
String message,
String fieldName,
int lineno,
int startPosition,
Exception exception,
TemplateErrorCategory category,
Map<String, String> categoryErrors,
int scopeDepth) {
this.severity = severity;
this.reason = reason;
this.item = item;
this.message = message;
this.fieldName = fieldName;
this.lineno = lineno;
this.startPosition = startPosition;
this.exception = exception;
this.category = category;
this.categoryErrors = categoryErrors;
this.scopeDepth = scopeDepth;
}


public TemplateError(ErrorType severity,
ErrorReason reason,
ErrorItem item,
Expand Down Expand Up @@ -246,8 +278,12 @@ public Map<String, String> getCategoryErrors() {
return categoryErrors;
}

public int getScopeDepth() {
return scopeDepth;
}

public TemplateError serializable() {
return new TemplateError(severity, reason, item, message, fieldName, lineno, startPosition, null, category, categoryErrors);
return new TemplateError(severity, reason, item, message, fieldName, lineno, startPosition, null, category, categoryErrors, scopeDepth);
}

@Override
Expand All @@ -260,6 +296,7 @@ public String toString() {
", fieldName='" + fieldName + '\'' +
", lineno=" + lineno +
", startPosition=" + startPosition +
", scopeDepth=" + scopeDepth +
", category=" + category +
", categoryErrors=" + categoryErrors +
'}';
Expand Down

0 comments on commit 03c35d2

Please sign in to comment.