Skip to content

Commit 06c4ec5

Browse files
Moved exception factory methods internally (#402)
Signed-off-by: Francesco Guardiani <[email protected]>
1 parent 73a3c37 commit 06c4ec5

14 files changed

+145
-119
lines changed

sql/src/main/java/io/cloudevents/sql/EvaluationException.java

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public enum ErrorKind {
4343
private final Interval interval;
4444
private final String expression;
4545

46-
protected EvaluationException(ErrorKind errorKind, Interval interval, String expression, String message, Throwable cause) {
46+
public EvaluationException(ErrorKind errorKind, Interval interval, String expression, String message, Throwable cause) {
4747
super(String.format("%s at %s `%s`: %s", errorKind.name(), interval.toString(), expression, message), cause);
4848
this.errorKind = errorKind;
4949
this.interval = interval;
@@ -62,63 +62,4 @@ public String getExpressionText() {
6262
return expression;
6363
}
6464

65-
public static EvaluationExceptionFactory invalidCastTarget(Class<?> from, Class<?> to) {
66-
return (interval, expression) -> new EvaluationException(
67-
ErrorKind.INVALID_CAST,
68-
interval,
69-
expression,
70-
"Cannot cast " + from + " to " + to + ": no cast defined.",
71-
null
72-
);
73-
}
74-
75-
public static EvaluationExceptionFactory castError(Class<?> from, Class<?> to, Throwable cause) {
76-
return (interval, expression) -> new EvaluationException(
77-
ErrorKind.INVALID_CAST,
78-
interval,
79-
expression,
80-
"Cannot cast " + from + " to " + to + ": " + cause.getMessage(),
81-
cause
82-
);
83-
}
84-
85-
public static EvaluationException missingAttribute(Interval interval, String expression, String key) {
86-
return new EvaluationException(
87-
ErrorKind.MISSING_ATTRIBUTE,
88-
interval,
89-
expression,
90-
"Missing attribute " + key + " in the input event. Perhaps you should check with 'EXISTS " + key + "' if the input contains the provided key?",
91-
null
92-
);
93-
}
94-
95-
public static EvaluationException cannotDispatchFunction(Interval interval, String expression, String functionName, Throwable cause) {
96-
return new EvaluationException(
97-
ErrorKind.FUNCTION_DISPATCH,
98-
interval,
99-
expression,
100-
"Cannot dispatch function invocation to function " + functionName + ": " + cause.getMessage(),
101-
cause
102-
);
103-
}
104-
105-
public static EvaluationExceptionFactory functionExecutionError(String functionName, Throwable cause) {
106-
return (interval, expression) -> new EvaluationException(
107-
ErrorKind.FUNCTION_EXECUTION,
108-
interval,
109-
expression,
110-
"Error while executing " + functionName + ": " + cause.getMessage(),
111-
cause
112-
);
113-
}
114-
115-
public static EvaluationException divisionByZero(Interval interval, String expression, Integer dividend) {
116-
return new EvaluationException(
117-
ErrorKind.MATH,
118-
interval,
119-
expression,
120-
"Division by zero: " + dividend + " / 0",
121-
null
122-
);
123-
}
12465
}
Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
11
package io.cloudevents.sql;
22

3-
import org.antlr.v4.runtime.RecognitionException;
43
import org.antlr.v4.runtime.misc.Interval;
5-
import org.antlr.v4.runtime.tree.ParseTree;
64

75
/**
86
* This exception represents an error occurred during parsing.
97
*/
108
public class ParseException extends RuntimeException {
119

1210
public enum ErrorKind {
11+
/**
12+
* Error when parsing the expression string
13+
*/
1314
RECOGNITION,
15+
/**
16+
* Error when parsing a literal
17+
*/
1418
PARSE_VALUE,
19+
/**
20+
* Error when executing the constant parts of the expression
21+
*/
1522
CONSTANT_EXPRESSION_EVALUATION,
1623
}
1724

1825
private final ErrorKind errorKind;
1926
private final Interval interval;
2027
private final String expression;
2128

22-
protected ParseException(ErrorKind errorKind, Interval interval, String expression, String message, Throwable cause) {
29+
public ParseException(ErrorKind errorKind, Interval interval, String expression, String message, Throwable cause) {
2330
super(String.format("[%s at %d:%d `%s`] %s", errorKind.name(), interval.a, interval.b, expression, message), cause);
2431
this.errorKind = errorKind;
2532
this.interval = interval;
@@ -38,34 +45,4 @@ public String getExpression() {
3845
return expression;
3946
}
4047

41-
public static ParseException cannotParseValue(ParseTree node, Type target, Throwable cause) {
42-
return new ParseException(
43-
ErrorKind.PARSE_VALUE,
44-
node.getSourceInterval(),
45-
node.getText(),
46-
"Cannot parse to " + target.name() + ": " + cause.getMessage(),
47-
cause
48-
);
49-
}
50-
51-
public static ParseException recognitionError(RecognitionException e, String msg) {
52-
return new ParseException(
53-
ErrorKind.RECOGNITION,
54-
new Interval(e.getOffendingToken().getStartIndex(), e.getOffendingToken().getStopIndex()),
55-
e.getOffendingToken().getText(),
56-
"Cannot parse: " + msg,
57-
e
58-
);
59-
}
60-
61-
public static ParseException cannotEvaluateConstantExpression(EvaluationException exception) {
62-
return new ParseException(
63-
ErrorKind.CONSTANT_EXPRESSION_EVALUATION,
64-
exception.getExpressionInterval(),
65-
exception.getExpressionText(),
66-
"Cannot evaluate the constant expression: " + exception.getExpressionText(),
67-
exception
68-
);
69-
}
70-
7148
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package io.cloudevents.sql.impl;
2+
3+
import io.cloudevents.sql.EvaluationException;
4+
import io.cloudevents.sql.ParseException;
5+
import io.cloudevents.sql.Type;
6+
import org.antlr.v4.runtime.RecognitionException;
7+
import org.antlr.v4.runtime.misc.Interval;
8+
import org.antlr.v4.runtime.tree.ParseTree;
9+
10+
/**
11+
* This class includes a list of static methods to create {@link io.cloudevents.sql.ParseException} and {@link io.cloudevents.sql.EvaluationException}.
12+
*/
13+
public class ExceptionFactory {
14+
15+
private ExceptionFactory() {
16+
}
17+
18+
public static EvaluationException.EvaluationExceptionFactory invalidCastTarget(Class<?> from, Class<?> to) {
19+
return (interval, expression) -> new EvaluationException(
20+
EvaluationException.ErrorKind.INVALID_CAST,
21+
interval,
22+
expression,
23+
"Cannot cast " + from + " to " + to + ": no cast defined.",
24+
null
25+
);
26+
}
27+
28+
public static EvaluationException.EvaluationExceptionFactory castError(Class<?> from, Class<?> to, Throwable cause) {
29+
return (interval, expression) -> new EvaluationException(
30+
EvaluationException.ErrorKind.INVALID_CAST,
31+
interval,
32+
expression,
33+
"Cannot cast " + from + " to " + to + ": " + cause.getMessage(),
34+
cause
35+
);
36+
}
37+
38+
public static EvaluationException missingAttribute(Interval interval, String expression, String key) {
39+
return new EvaluationException(
40+
EvaluationException.ErrorKind.MISSING_ATTRIBUTE,
41+
interval,
42+
expression,
43+
"Missing attribute " + key + " in the input event. Perhaps you should check with 'EXISTS " + key + "' if the input contains the provided key?",
44+
null
45+
);
46+
}
47+
48+
public static EvaluationException cannotDispatchFunction(Interval interval, String expression, String functionName, Throwable cause) {
49+
return new EvaluationException(
50+
EvaluationException.ErrorKind.FUNCTION_DISPATCH,
51+
interval,
52+
expression,
53+
"Cannot dispatch function invocation to function " + functionName + ": " + cause.getMessage(),
54+
cause
55+
);
56+
}
57+
58+
public static EvaluationException.EvaluationExceptionFactory functionExecutionError(String functionName, Throwable cause) {
59+
return (interval, expression) -> new EvaluationException(
60+
EvaluationException.ErrorKind.FUNCTION_EXECUTION,
61+
interval,
62+
expression,
63+
"Error while executing " + functionName + ": " + cause.getMessage(),
64+
cause
65+
);
66+
}
67+
68+
public static EvaluationException divisionByZero(Interval interval, String expression, Integer dividend) {
69+
return new EvaluationException(
70+
EvaluationException.ErrorKind.MATH,
71+
interval,
72+
expression,
73+
"Division by zero: " + dividend + " / 0",
74+
null
75+
);
76+
}
77+
78+
public static ParseException cannotParseValue(ParseTree node, Type target, Throwable cause) {
79+
return new ParseException(
80+
ParseException.ErrorKind.PARSE_VALUE,
81+
node.getSourceInterval(),
82+
node.getText(),
83+
"Cannot parse to " + target.name() + ": " + cause.getMessage(),
84+
cause
85+
);
86+
}
87+
88+
public static ParseException recognitionError(RecognitionException e, String msg) {
89+
return new ParseException(
90+
ParseException.ErrorKind.RECOGNITION,
91+
new Interval(e.getOffendingToken().getStartIndex(), e.getOffendingToken().getStopIndex()),
92+
e.getOffendingToken().getText(),
93+
"Cannot parse: " + msg,
94+
e
95+
);
96+
}
97+
98+
public static ParseException cannotEvaluateConstantExpression(EvaluationException exception) {
99+
return new ParseException(
100+
ParseException.ErrorKind.CONSTANT_EXPRESSION_EVALUATION,
101+
exception.getExpressionInterval(),
102+
exception.getExpressionText(),
103+
"Cannot evaluate the constant expression: " + exception.getExpressionText(),
104+
exception
105+
);
106+
}
107+
}

sql/src/main/java/io/cloudevents/sql/impl/expressions/AccessAttributeExpression.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import io.cloudevents.CloudEvent;
44
import io.cloudevents.SpecVersion;
5-
import io.cloudevents.sql.EvaluationException;
65
import io.cloudevents.sql.EvaluationRuntime;
6+
import io.cloudevents.sql.impl.ExceptionFactory;
77
import io.cloudevents.sql.impl.ExceptionThrower;
88
import io.cloudevents.sql.impl.ExpressionInternalVisitor;
99
import org.antlr.v4.runtime.misc.Interval;
@@ -28,7 +28,7 @@ public Object evaluate(EvaluationRuntime runtime, CloudEvent event, ExceptionThr
2828
Object value = this.getter.apply(event);
2929
if (value == null) {
3030
thrower.throwException(
31-
EvaluationException.missingAttribute(this.expressionInterval(), this.expressionText(), key)
31+
ExceptionFactory.missingAttribute(this.expressionInterval(), this.expressionText(), key)
3232
);
3333
return "";
3434
}

sql/src/main/java/io/cloudevents/sql/impl/expressions/DivisionExpression.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.cloudevents.sql.impl.expressions;
22

3-
import io.cloudevents.sql.EvaluationException;
43
import io.cloudevents.sql.EvaluationRuntime;
4+
import io.cloudevents.sql.impl.ExceptionFactory;
55
import io.cloudevents.sql.impl.ExceptionThrower;
66
import io.cloudevents.sql.impl.ExpressionInternal;
77
import org.antlr.v4.runtime.misc.Interval;
@@ -16,7 +16,7 @@ public DivisionExpression(Interval expressionInterval, String expressionText, Ex
1616
Object evaluate(EvaluationRuntime runtime, int left, int right, ExceptionThrower exceptions) {
1717
if (right == 0) {
1818
exceptions.throwException(
19-
EvaluationException.divisionByZero(expressionInterval(), expressionText(), left)
19+
ExceptionFactory.divisionByZero(expressionInterval(), expressionText(), left)
2020
);
2121
return 0;
2222
}

sql/src/main/java/io/cloudevents/sql/impl/expressions/FunctionInvocationExpression.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import io.cloudevents.CloudEvent;
44
import io.cloudevents.sql.EvaluationContext;
5-
import io.cloudevents.sql.EvaluationException;
65
import io.cloudevents.sql.EvaluationRuntime;
76
import io.cloudevents.sql.Function;
7+
import io.cloudevents.sql.impl.ExceptionFactory;
88
import io.cloudevents.sql.impl.ExceptionThrower;
99
import io.cloudevents.sql.impl.ExpressionInternal;
1010
import io.cloudevents.sql.impl.ExpressionInternalVisitor;
@@ -34,7 +34,7 @@ public Object evaluate(EvaluationRuntime runtime, CloudEvent event, ExceptionThr
3434
function = runtime.resolveFunction(functionName, arguments.size());
3535
} catch (Exception e) {
3636
thrower.throwException(
37-
EvaluationException.cannotDispatchFunction(expressionInterval(), expressionText(), functionName, e)
37+
ExceptionFactory.cannotDispatchFunction(expressionInterval(), expressionText(), functionName, e)
3838
);
3939
return "";
4040
}

sql/src/main/java/io/cloudevents/sql/impl/expressions/ModuleExpression.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.cloudevents.sql.impl.expressions;
22

3-
import io.cloudevents.sql.EvaluationException;
43
import io.cloudevents.sql.EvaluationRuntime;
4+
import io.cloudevents.sql.impl.ExceptionFactory;
55
import io.cloudevents.sql.impl.ExceptionThrower;
66
import io.cloudevents.sql.impl.ExpressionInternal;
77
import org.antlr.v4.runtime.misc.Interval;
@@ -16,7 +16,7 @@ public ModuleExpression(Interval expressionInterval, String expressionText, Expr
1616
Object evaluate(EvaluationRuntime runtime, int left, int right, ExceptionThrower exceptions) {
1717
if (right == 0) {
1818
exceptions.throwException(
19-
EvaluationException.divisionByZero(expressionInterval(), expressionText(), left)
19+
ExceptionFactory.divisionByZero(expressionInterval(), expressionText(), left)
2020
);
2121
return 0;
2222
}

sql/src/main/java/io/cloudevents/sql/impl/functions/LeftFunction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import io.cloudevents.CloudEvent;
44
import io.cloudevents.sql.EvaluationContext;
5-
import io.cloudevents.sql.EvaluationException;
65
import io.cloudevents.sql.EvaluationRuntime;
6+
import io.cloudevents.sql.impl.ExceptionFactory;
77

88
public class LeftFunction extends BaseTwoArgumentFunction<String, Integer> {
99
public LeftFunction() {
@@ -17,7 +17,7 @@ Object invoke(EvaluationContext ctx, EvaluationRuntime evaluationRuntime, CloudE
1717
}
1818
if (length < 0) {
1919
ctx.appendException(
20-
EvaluationException.functionExecutionError(name(), new IllegalArgumentException("The length of the LEFT substring is lower than 0: " + length))
20+
ExceptionFactory.functionExecutionError(name(), new IllegalArgumentException("The length of the LEFT substring is lower than 0: " + length))
2121
);
2222
return s;
2323
}

sql/src/main/java/io/cloudevents/sql/impl/functions/RightFunction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import io.cloudevents.CloudEvent;
44
import io.cloudevents.sql.EvaluationContext;
5-
import io.cloudevents.sql.EvaluationException;
65
import io.cloudevents.sql.EvaluationRuntime;
6+
import io.cloudevents.sql.impl.ExceptionFactory;
77

88
public class RightFunction extends BaseTwoArgumentFunction<String, Integer> {
99
public RightFunction() {
@@ -17,7 +17,7 @@ Object invoke(EvaluationContext ctx, EvaluationRuntime evaluationRuntime, CloudE
1717
}
1818
if (length < 0) {
1919
ctx.appendException(
20-
EvaluationException.functionExecutionError(name(), new IllegalArgumentException("The length of the RIGHT substring is lower than 0: " + length))
20+
ExceptionFactory.functionExecutionError(name(), new IllegalArgumentException("The length of the RIGHT substring is lower than 0: " + length))
2121
);
2222
return s;
2323
}

sql/src/main/java/io/cloudevents/sql/impl/functions/SubstringFunction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import io.cloudevents.CloudEvent;
44
import io.cloudevents.sql.EvaluationContext;
5-
import io.cloudevents.sql.EvaluationException;
65
import io.cloudevents.sql.EvaluationRuntime;
6+
import io.cloudevents.sql.impl.ExceptionFactory;
77

88
public class SubstringFunction extends BaseTwoArgumentFunction<String, Integer> {
99
public SubstringFunction() {
@@ -15,7 +15,7 @@ Object invoke(EvaluationContext ctx, EvaluationRuntime evaluationRuntime, CloudE
1515
try {
1616
return SubstringWithLengthFunction.substring(x, pos, null);
1717
} catch (Exception e) {
18-
ctx.appendException(EvaluationException.functionExecutionError(
18+
ctx.appendException(ExceptionFactory.functionExecutionError(
1919
name(),
2020
e
2121
));

0 commit comments

Comments
 (0)