-
Notifications
You must be signed in to change notification settings - Fork 214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix null pointer exception during exception evaluation #5217
Conversation
Signed-off-by: Krishna Kondaka <[email protected]>
@@ -102,7 +102,6 @@ public Collection<Record<Event>> doExecute(final Collection<Record<Event>> recor | |||
.addArgument(entry.getValueExpression()) | |||
.addArgument(entry.getFormat()) | |||
.addArgument(entry.getValue()) | |||
.setCause(e) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should include the cause whenever we catch Exception
. Please add a new catch for the known exception(s) when these errors arise.
e.g.
catch (InvalidExpressionException e) {
// log without exception
} catch (Exception e) {
// log with the exception
}
try { | ||
result = evaluate(statement, context); | ||
if (result == null) { | ||
return false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that this is correct. This indicates a bug with our exception handling because any boolean expression should return non-null. Maybe we need to handle this by throwing a more specific exception. Or perhaps we log this case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we cover all the cases in the code below. I think we should still consider this an error. If we hit this point we are experiencing indeterminate behavior which is probably best considered as an error state.
@@ -37,7 +37,7 @@ public Object evaluate(ParseTree parseTree, Event event) { | |||
walker.walk(listener, parseTree); | |||
return listener.getResult(); | |||
} catch (final Exception e) { | |||
LOG.error("Unable to evaluate event", e); | |||
LOG.error(e.getMessage()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should log the stack trace when catching Exception
.
Please add new catch statements for known exceptions (e.g. parse error) and log those without the stack trace.
@@ -119,7 +118,7 @@ void testGenericExpressionEvaluatorWithMultipleThreads(final String expression, | |||
void testGenericExpressionEvaluatorThrows(final String expression, final Event event) { | |||
final GenericExpressionEvaluator evaluator = applicationContext.getBean(GenericExpressionEvaluator.class); | |||
|
|||
assertThrows(RuntimeException.class, () -> evaluator.evaluateConditional(expression, event)); | |||
assertThat(evaluator.evaluateConditional(expression, event), equalTo(false)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This behavior does not seem correct.
Signed-off-by: Krishna Kondaka <[email protected]>
@@ -112,6 +112,18 @@ public void visitErrorNode(ErrorNode node) { | |||
throw new RuntimeException("Hit error node in the parse tree: " + node.getText()); | |||
} | |||
|
|||
private boolean isBooleanOperator(Operator<?> op) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this will scale. We should add this as a method in Operator
that needs to be implemented. This way, each implementation is sure to have some value for it.
try { | ||
result = evaluate(statement, context); | ||
if (result == null) { | ||
return false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we cover all the cases in the code below. I think we should still consider this an error. If we hit this point we are experiencing indeterminate behavior which is probably best considered as an error state.
…parsing exceptions Signed-off-by: Krishna Kondaka <[email protected]>
Arguments.of("/status - ", event("{\"status\": 200, \"message\":\"msg\"}")), | ||
Arguments.of("/status / ", event("{\"status\": 200, \"message\":\"msg\"}")), | ||
Arguments.of(" * /status ", event("{\"status\": 200, \"message\":\"msg\"}")) | ||
//Arguments.of("-/message ", event("{\"status\": 200, \"message\":\"msg\"}")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's remove this commented out code.
Arguments.of("/status - ", event("{\"status\": 200, \"message\":\"msg\"}")), | ||
Arguments.of("/status / ", event("{\"status\": 200, \"message\":\"msg\"}")), | ||
Arguments.of(" * /status ", event("{\"status\": 200, \"message\":\"msg\"}")), | ||
//Arguments.of("/status - ", event("{\"status\": 200, \"message\":\"msg\"}")), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's remove this commented out code.
* @since 2.11 | ||
* Wrapper exception for any exception thrown while evaluating a statement | ||
*/ | ||
public class ExpressionParsingException extends RuntimeException { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like having this specific exception. But, I think we should have a common parent, that parent should probably remain ExpressionEvaluationException
.
public class ExpressionParsingException extends RuntimeException { | |
public class ExpressionParsingException extends ExpressionEvaluationException { |
We could possibly add other sub-classes to ExpressionEvaluationException
for other cases in the future as well.
return evaluator.evaluate(parseTree, context); | ||
} | ||
catch (final Exception exception) { | ||
throw new ExpressionEvaluationException("Unable to evaluate statement \"" + statement + "\"", exception); | ||
if (parsedSuccessfully) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can avoid the boolean. This should also be easier to maintain as we clearly distinguish the steps.
final ParseTree parseTree = parser.parse(statement);
try {
parseTree = parser.parse(statement);
} catch (final Exception exception) {
throw new ExpressionParsingException("Unable to parse statement \"" + statement + "\"", exception);
}
try {
return evaluator.evaluate(parseTree, context);
} catch (final Exception exception) {
throw new ExpressionEvaluationException("Unable to evaluate statement \"" + statement + "\"", exception);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I thought about this but felt one try catch may be better. Anyways, changed to this.
if (result instanceof Boolean) { | ||
return (Boolean) result; | ||
} | ||
throw new ClassCastException("Unexpected expression return value of " + result); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this PR raises the idea of other exceptions, this is a good candidate for a better exception.
Declare:
public class UnexpectedEvaluatedReturnTypeException extends ExpressionEvaluationException {
And then throw:
throw new UnexpectedEvaluatedReturnTypeException("Unexpected expression return value of " + result);
We don't have to do for this PR, but it would be a good improvement.
Signed-off-by: Krishna Kondaka <[email protected]>
…oject#5217) Fix null pointer exception during exception evaluation Signed-off-by: Krishna Kondaka <[email protected]>
…oject#5217) Fix null pointer exception during exception evaluation Signed-off-by: Krishna Kondaka <[email protected]>
Description
Fix null pointer exception during exception evaluation.
Modified to check for null objects before evaluating expressions.
Modified to catch exception in conditional expression evaluation and return false instead of throwing the exception
Issues Resolved
Resolves #[Issue number to be closed when this PR is merged]
Check List
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.