refactor FHIRPath to report error locations for run time errors

This commit is contained in:
Grahame Grieve 2020-09-30 12:10:59 +10:00
parent 6407f65353
commit 46d1987cd0
9 changed files with 684 additions and 649 deletions

View File

@ -34,6 +34,7 @@ package org.hl7.fhir.r4.model;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.hl7.fhir.utilities.SourceLocation;
import org.hl7.fhir.utilities.Utilities; import org.hl7.fhir.utilities.Utilities;
public class ExpressionNode { public class ExpressionNode {
@ -41,31 +42,6 @@ public class ExpressionNode {
public enum Kind { public enum Kind {
Name, Function, Constant, Group, Unary Name, Function, Constant, Group, Unary
} }
public static class SourceLocation {
private int line;
private int column;
public SourceLocation(int line, int column) {
super();
this.line = line;
this.column = column;
}
public int getLine() {
return line;
}
public int getColumn() {
return column;
}
public void setLine(int line) {
this.line = line;
}
public void setColumn(int column) {
this.column = column;
}
public String toString() {
return Integer.toString(line)+", "+Integer.toString(column);
}
}
public enum Function { public enum Function {
Custom, Custom,

View File

@ -34,7 +34,7 @@ import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.r4.model.ExpressionNode; import org.hl7.fhir.r4.model.ExpressionNode;
import org.hl7.fhir.r4.model.ExpressionNode.SourceLocation; import org.hl7.fhir.utilities.SourceLocation;
import org.hl7.fhir.utilities.Utilities; import org.hl7.fhir.utilities.Utilities;
// shared lexer for concrete syntaxes // shared lexer for concrete syntaxes

View File

@ -34,6 +34,7 @@ package org.hl7.fhir.r5.model;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.hl7.fhir.utilities.SourceLocation;
import org.hl7.fhir.utilities.Utilities; import org.hl7.fhir.utilities.Utilities;
public class ExpressionNode { public class ExpressionNode {
@ -41,31 +42,7 @@ public class ExpressionNode {
public enum Kind { public enum Kind {
Name, Function, Constant, Group, Unary Name, Function, Constant, Group, Unary
} }
public static class SourceLocation {
private int line;
private int column;
public SourceLocation(int line, int column) {
super();
this.line = line;
this.column = column;
}
public int getLine() {
return line;
}
public int getColumn() {
return column;
}
public void setLine(int line) {
this.line = line;
}
public void setColumn(int column) {
this.column = column;
}
public String toString() {
return Integer.toString(line)+", "+Integer.toString(column);
}
}
public enum Function { public enum Function {
Custom, Custom,

View File

@ -34,7 +34,7 @@ import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.r5.model.ExpressionNode; import org.hl7.fhir.r5.model.ExpressionNode;
import org.hl7.fhir.r5.model.ExpressionNode.SourceLocation; import org.hl7.fhir.utilities.SourceLocation;
import org.hl7.fhir.utilities.Utilities; import org.hl7.fhir.utilities.Utilities;
// shared lexer for concrete syntaxes // shared lexer for concrete syntaxes

View File

@ -176,6 +176,7 @@ public class FHIRPathTests {
node = fp.parse(expression); node = fp.parse(expression);
Assertions.assertTrue(fail != TestResultType.SYNTAX, String.format("Expected exception didn't occur parsing %s", expression)); Assertions.assertTrue(fail != TestResultType.SYNTAX, String.format("Expected exception didn't occur parsing %s", expression));
} catch (Exception e) { } catch (Exception e) {
System.out.println("Parsing Error: "+e.getMessage());
Assertions.assertTrue(fail == TestResultType.SYNTAX, String.format("Unexpected exception parsing %s: " + e.getMessage(), expression)); Assertions.assertTrue(fail == TestResultType.SYNTAX, String.format("Unexpected exception parsing %s: " + e.getMessage(), expression));
} }
@ -193,6 +194,7 @@ public class FHIRPathTests {
} }
Assertions.assertTrue(fail != TestResultType.SEMANTICS, String.format("Expected exception didn't occur checking %s", expression)); Assertions.assertTrue(fail != TestResultType.SEMANTICS, String.format("Expected exception didn't occur checking %s", expression));
} catch (Exception e) { } catch (Exception e) {
System.out.println("Checking Error: "+e.getMessage());
Assertions.assertTrue(fail == TestResultType.SEMANTICS, String.format("Unexpected exception checking %s: " + e.getMessage(), expression)); Assertions.assertTrue(fail == TestResultType.SEMANTICS, String.format("Unexpected exception checking %s: " + e.getMessage(), expression));
node = null; node = null;
} }
@ -203,6 +205,7 @@ public class FHIRPathTests {
outcome = fp.evaluate(res, node); outcome = fp.evaluate(res, node);
Assertions.assertTrue(fail == TestResultType.OK, String.format("Expected exception didn't occur executing %s", expression)); Assertions.assertTrue(fail == TestResultType.OK, String.format("Expected exception didn't occur executing %s", expression));
} catch (Exception e) { } catch (Exception e) {
System.out.println("Execution Error: "+e.getMessage());
Assertions.assertTrue(fail == TestResultType.EXECUTION, String.format("Unexpected exception executing %s: " + e.getMessage(), expression)); Assertions.assertTrue(fail == TestResultType.EXECUTION, String.format("Unexpected exception executing %s: " + e.getMessage(), expression));
node = null; node = null;
} }

View File

@ -1,5 +1,8 @@
package org.hl7.fhir.exceptions; package org.hl7.fhir.exceptions;
import org.hl7.fhir.utilities.SourceLocation;
import org.hl7.fhir.utilities.Utilities;
/* /*
Copyright (c) 2011+, HL7, Inc. Copyright (c) 2011+, HL7, Inc.
All rights reserved. All rights reserved.
@ -33,6 +36,10 @@ package org.hl7.fhir.exceptions;
public class PathEngineException extends FHIRException { public class PathEngineException extends FHIRException {
private static final long serialVersionUID = 31969342112856390L;
private SourceLocation location;
private String expression;
public PathEngineException() { public PathEngineException() {
super(); super();
} }
@ -45,8 +52,46 @@ public class PathEngineException extends FHIRException {
super(message); super(message);
} }
public PathEngineException(String message, SourceLocation location, String expression, Throwable cause) {
super(message+rep(location, expression), cause);
}
public PathEngineException(String message, SourceLocation location, String expression) {
super(message+rep(location, expression));
}
private static String rep(SourceLocation loc, String expr) {
if (loc != null) {
if (loc.getLine() == 1) {
return " (@char "+loc.getColumn()+")";
} else {
return " (@line "+loc.getLine()+" char "+loc.getColumn()+")";
}
} else if (Utilities.noString(expr)) { // can happen in some contexts...
return " (@~"+expr+")";
} else {
return "";
}
}
public PathEngineException(Throwable cause) { public PathEngineException(Throwable cause) {
super(cause); super(cause);
} }
public String getExpression() {
return expression;
}
public void setExpression(String expression) {
this.expression = expression;
}
public SourceLocation getLocation() {
return location;
}
public void setLocation(SourceLocation location) {
this.location = location;
}
} }

View File

@ -0,0 +1,27 @@
package org.hl7.fhir.utilities;
public class SourceLocation {
private int line;
private int column;
public SourceLocation(int line, int column) {
super();
this.line = line;
this.column = column;
}
public int getLine() {
return line;
}
public int getColumn() {
return column;
}
public void setLine(int line) {
this.line = line;
}
public void setColumn(int column) {
this.column = column;
}
public String toString() {
return Integer.toString(line)+", "+Integer.toString(column);
}
}