Skip to content

Commit

Permalink
Moved exception factory methods internally (#402)
Browse files Browse the repository at this point in the history
Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>
  • Loading branch information
slinkydeveloper authored Jul 5, 2021
1 parent 73a3c37 commit 06c4ec5
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 119 deletions.
61 changes: 1 addition & 60 deletions sql/src/main/java/io/cloudevents/sql/EvaluationException.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public enum ErrorKind {
private final Interval interval;
private final String expression;

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

public static EvaluationExceptionFactory invalidCastTarget(Class<?> from, Class<?> to) {
return (interval, expression) -> new EvaluationException(
ErrorKind.INVALID_CAST,
interval,
expression,
"Cannot cast " + from + " to " + to + ": no cast defined.",
null
);
}

public static EvaluationExceptionFactory castError(Class<?> from, Class<?> to, Throwable cause) {
return (interval, expression) -> new EvaluationException(
ErrorKind.INVALID_CAST,
interval,
expression,
"Cannot cast " + from + " to " + to + ": " + cause.getMessage(),
cause
);
}

public static EvaluationException missingAttribute(Interval interval, String expression, String key) {
return new EvaluationException(
ErrorKind.MISSING_ATTRIBUTE,
interval,
expression,
"Missing attribute " + key + " in the input event. Perhaps you should check with 'EXISTS " + key + "' if the input contains the provided key?",
null
);
}

public static EvaluationException cannotDispatchFunction(Interval interval, String expression, String functionName, Throwable cause) {
return new EvaluationException(
ErrorKind.FUNCTION_DISPATCH,
interval,
expression,
"Cannot dispatch function invocation to function " + functionName + ": " + cause.getMessage(),
cause
);
}

public static EvaluationExceptionFactory functionExecutionError(String functionName, Throwable cause) {
return (interval, expression) -> new EvaluationException(
ErrorKind.FUNCTION_EXECUTION,
interval,
expression,
"Error while executing " + functionName + ": " + cause.getMessage(),
cause
);
}

public static EvaluationException divisionByZero(Interval interval, String expression, Integer dividend) {
return new EvaluationException(
ErrorKind.MATH,
interval,
expression,
"Division by zero: " + dividend + " / 0",
null
);
}
}
43 changes: 10 additions & 33 deletions sql/src/main/java/io/cloudevents/sql/ParseException.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
package io.cloudevents.sql;

import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.misc.Interval;
import org.antlr.v4.runtime.tree.ParseTree;

/**
* This exception represents an error occurred during parsing.
*/
public class ParseException extends RuntimeException {

public enum ErrorKind {
/**
* Error when parsing the expression string
*/
RECOGNITION,
/**
* Error when parsing a literal
*/
PARSE_VALUE,
/**
* Error when executing the constant parts of the expression
*/
CONSTANT_EXPRESSION_EVALUATION,
}

private final ErrorKind errorKind;
private final Interval interval;
private final String expression;

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

public static ParseException cannotParseValue(ParseTree node, Type target, Throwable cause) {
return new ParseException(
ErrorKind.PARSE_VALUE,
node.getSourceInterval(),
node.getText(),
"Cannot parse to " + target.name() + ": " + cause.getMessage(),
cause
);
}

public static ParseException recognitionError(RecognitionException e, String msg) {
return new ParseException(
ErrorKind.RECOGNITION,
new Interval(e.getOffendingToken().getStartIndex(), e.getOffendingToken().getStopIndex()),
e.getOffendingToken().getText(),
"Cannot parse: " + msg,
e
);
}

public static ParseException cannotEvaluateConstantExpression(EvaluationException exception) {
return new ParseException(
ErrorKind.CONSTANT_EXPRESSION_EVALUATION,
exception.getExpressionInterval(),
exception.getExpressionText(),
"Cannot evaluate the constant expression: " + exception.getExpressionText(),
exception
);
}

}
107 changes: 107 additions & 0 deletions sql/src/main/java/io/cloudevents/sql/impl/ExceptionFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package io.cloudevents.sql.impl;

import io.cloudevents.sql.EvaluationException;
import io.cloudevents.sql.ParseException;
import io.cloudevents.sql.Type;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.misc.Interval;
import org.antlr.v4.runtime.tree.ParseTree;

/**
* This class includes a list of static methods to create {@link io.cloudevents.sql.ParseException} and {@link io.cloudevents.sql.EvaluationException}.
*/
public class ExceptionFactory {

private ExceptionFactory() {
}

public static EvaluationException.EvaluationExceptionFactory invalidCastTarget(Class<?> from, Class<?> to) {
return (interval, expression) -> new EvaluationException(
EvaluationException.ErrorKind.INVALID_CAST,
interval,
expression,
"Cannot cast " + from + " to " + to + ": no cast defined.",
null
);
}

public static EvaluationException.EvaluationExceptionFactory castError(Class<?> from, Class<?> to, Throwable cause) {
return (interval, expression) -> new EvaluationException(
EvaluationException.ErrorKind.INVALID_CAST,
interval,
expression,
"Cannot cast " + from + " to " + to + ": " + cause.getMessage(),
cause
);
}

public static EvaluationException missingAttribute(Interval interval, String expression, String key) {
return new EvaluationException(
EvaluationException.ErrorKind.MISSING_ATTRIBUTE,
interval,
expression,
"Missing attribute " + key + " in the input event. Perhaps you should check with 'EXISTS " + key + "' if the input contains the provided key?",
null
);
}

public static EvaluationException cannotDispatchFunction(Interval interval, String expression, String functionName, Throwable cause) {
return new EvaluationException(
EvaluationException.ErrorKind.FUNCTION_DISPATCH,
interval,
expression,
"Cannot dispatch function invocation to function " + functionName + ": " + cause.getMessage(),
cause
);
}

public static EvaluationException.EvaluationExceptionFactory functionExecutionError(String functionName, Throwable cause) {
return (interval, expression) -> new EvaluationException(
EvaluationException.ErrorKind.FUNCTION_EXECUTION,
interval,
expression,
"Error while executing " + functionName + ": " + cause.getMessage(),
cause
);
}

public static EvaluationException divisionByZero(Interval interval, String expression, Integer dividend) {
return new EvaluationException(
EvaluationException.ErrorKind.MATH,
interval,
expression,
"Division by zero: " + dividend + " / 0",
null
);
}

public static ParseException cannotParseValue(ParseTree node, Type target, Throwable cause) {
return new ParseException(
ParseException.ErrorKind.PARSE_VALUE,
node.getSourceInterval(),
node.getText(),
"Cannot parse to " + target.name() + ": " + cause.getMessage(),
cause
);
}

public static ParseException recognitionError(RecognitionException e, String msg) {
return new ParseException(
ParseException.ErrorKind.RECOGNITION,
new Interval(e.getOffendingToken().getStartIndex(), e.getOffendingToken().getStopIndex()),
e.getOffendingToken().getText(),
"Cannot parse: " + msg,
e
);
}

public static ParseException cannotEvaluateConstantExpression(EvaluationException exception) {
return new ParseException(
ParseException.ErrorKind.CONSTANT_EXPRESSION_EVALUATION,
exception.getExpressionInterval(),
exception.getExpressionText(),
"Cannot evaluate the constant expression: " + exception.getExpressionText(),
exception
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import io.cloudevents.CloudEvent;
import io.cloudevents.SpecVersion;
import io.cloudevents.sql.EvaluationException;
import io.cloudevents.sql.EvaluationRuntime;
import io.cloudevents.sql.impl.ExceptionFactory;
import io.cloudevents.sql.impl.ExceptionThrower;
import io.cloudevents.sql.impl.ExpressionInternalVisitor;
import org.antlr.v4.runtime.misc.Interval;
Expand All @@ -28,7 +28,7 @@ public Object evaluate(EvaluationRuntime runtime, CloudEvent event, ExceptionThr
Object value = this.getter.apply(event);
if (value == null) {
thrower.throwException(
EvaluationException.missingAttribute(this.expressionInterval(), this.expressionText(), key)
ExceptionFactory.missingAttribute(this.expressionInterval(), this.expressionText(), key)
);
return "";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.cloudevents.sql.impl.expressions;

import io.cloudevents.sql.EvaluationException;
import io.cloudevents.sql.EvaluationRuntime;
import io.cloudevents.sql.impl.ExceptionFactory;
import io.cloudevents.sql.impl.ExceptionThrower;
import io.cloudevents.sql.impl.ExpressionInternal;
import org.antlr.v4.runtime.misc.Interval;
Expand All @@ -16,7 +16,7 @@ public DivisionExpression(Interval expressionInterval, String expressionText, Ex
Object evaluate(EvaluationRuntime runtime, int left, int right, ExceptionThrower exceptions) {
if (right == 0) {
exceptions.throwException(
EvaluationException.divisionByZero(expressionInterval(), expressionText(), left)
ExceptionFactory.divisionByZero(expressionInterval(), expressionText(), left)
);
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import io.cloudevents.CloudEvent;
import io.cloudevents.sql.EvaluationContext;
import io.cloudevents.sql.EvaluationException;
import io.cloudevents.sql.EvaluationRuntime;
import io.cloudevents.sql.Function;
import io.cloudevents.sql.impl.ExceptionFactory;
import io.cloudevents.sql.impl.ExceptionThrower;
import io.cloudevents.sql.impl.ExpressionInternal;
import io.cloudevents.sql.impl.ExpressionInternalVisitor;
Expand Down Expand Up @@ -34,7 +34,7 @@ public Object evaluate(EvaluationRuntime runtime, CloudEvent event, ExceptionThr
function = runtime.resolveFunction(functionName, arguments.size());
} catch (Exception e) {
thrower.throwException(
EvaluationException.cannotDispatchFunction(expressionInterval(), expressionText(), functionName, e)
ExceptionFactory.cannotDispatchFunction(expressionInterval(), expressionText(), functionName, e)
);
return "";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.cloudevents.sql.impl.expressions;

import io.cloudevents.sql.EvaluationException;
import io.cloudevents.sql.EvaluationRuntime;
import io.cloudevents.sql.impl.ExceptionFactory;
import io.cloudevents.sql.impl.ExceptionThrower;
import io.cloudevents.sql.impl.ExpressionInternal;
import org.antlr.v4.runtime.misc.Interval;
Expand All @@ -16,7 +16,7 @@ public ModuleExpression(Interval expressionInterval, String expressionText, Expr
Object evaluate(EvaluationRuntime runtime, int left, int right, ExceptionThrower exceptions) {
if (right == 0) {
exceptions.throwException(
EvaluationException.divisionByZero(expressionInterval(), expressionText(), left)
ExceptionFactory.divisionByZero(expressionInterval(), expressionText(), left)
);
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import io.cloudevents.CloudEvent;
import io.cloudevents.sql.EvaluationContext;
import io.cloudevents.sql.EvaluationException;
import io.cloudevents.sql.EvaluationRuntime;
import io.cloudevents.sql.impl.ExceptionFactory;

public class LeftFunction extends BaseTwoArgumentFunction<String, Integer> {
public LeftFunction() {
Expand All @@ -17,7 +17,7 @@ Object invoke(EvaluationContext ctx, EvaluationRuntime evaluationRuntime, CloudE
}
if (length < 0) {
ctx.appendException(
EvaluationException.functionExecutionError(name(), new IllegalArgumentException("The length of the LEFT substring is lower than 0: " + length))
ExceptionFactory.functionExecutionError(name(), new IllegalArgumentException("The length of the LEFT substring is lower than 0: " + length))
);
return s;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import io.cloudevents.CloudEvent;
import io.cloudevents.sql.EvaluationContext;
import io.cloudevents.sql.EvaluationException;
import io.cloudevents.sql.EvaluationRuntime;
import io.cloudevents.sql.impl.ExceptionFactory;

public class RightFunction extends BaseTwoArgumentFunction<String, Integer> {
public RightFunction() {
Expand All @@ -17,7 +17,7 @@ Object invoke(EvaluationContext ctx, EvaluationRuntime evaluationRuntime, CloudE
}
if (length < 0) {
ctx.appendException(
EvaluationException.functionExecutionError(name(), new IllegalArgumentException("The length of the RIGHT substring is lower than 0: " + length))
ExceptionFactory.functionExecutionError(name(), new IllegalArgumentException("The length of the RIGHT substring is lower than 0: " + length))
);
return s;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import io.cloudevents.CloudEvent;
import io.cloudevents.sql.EvaluationContext;
import io.cloudevents.sql.EvaluationException;
import io.cloudevents.sql.EvaluationRuntime;
import io.cloudevents.sql.impl.ExceptionFactory;

public class SubstringFunction extends BaseTwoArgumentFunction<String, Integer> {
public SubstringFunction() {
Expand All @@ -15,7 +15,7 @@ Object invoke(EvaluationContext ctx, EvaluationRuntime evaluationRuntime, CloudE
try {
return SubstringWithLengthFunction.substring(x, pos, null);
} catch (Exception e) {
ctx.appendException(EvaluationException.functionExecutionError(
ctx.appendException(ExceptionFactory.functionExecutionError(
name(),
e
));
Expand Down
Loading

0 comments on commit 06c4ec5

Please sign in to comment.