Skip to content

Commit

Permalink
Add error handling improvements
Browse files Browse the repository at this point in the history
Add error handling improvements to synapse expressions
  • Loading branch information
GDLMadushanka committed Feb 13, 2025
1 parent 906ef7c commit cdfdf23
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,8 @@ arrayIndex
| expression
| multipleArrayIndices
| sliceArrayIndex
| expression ( (PLUS | MINUS | ASTERISK | DIV ) expression)*
| ASTERISK
| QUESTION? filterExpression
| QUESTION? LPAREN filterExpression RPAREN
;

multipleArrayIndices
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ public ExpressionResult evaluate(EvaluationContext context, boolean isObjectValu
}
//TODO: Need to stop adding "?" for expressions like $..book[(@.length-1)].title. But not handling this for
// now since its not even working in json-path.
return new ExpressionResult("?" + expression);
return new ExpressionResult("?(" + expression + ")");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.synapse.SynapseConstants;
import org.apache.synapse.util.synapse.expression.ast.*;
import org.apache.synapse.util.synapse.expression.constants.ExpressionConstants;
import org.apache.synapse.util.synapse.expression.exception.EvaluationException;
import org.apache.synapse.util.synapse_expression.ExpressionParser;
import org.apache.synapse.util.synapse_expression.ExpressionParserBaseVisitor;
import org.apache.synapse.util.synapse_expression.ExpressionParserVisitor;
Expand Down Expand Up @@ -49,7 +50,7 @@ public ExpressionNode visitExpression(ExpressionParser.ExpressionContext ctx) {
} else if (ctx.conditionalExpression() != null) {
return visitConditionalExpression(ctx.conditionalExpression());
}
return null;
throw new EvaluationException("Invalid expression: " + ctx.getText());
}

@Override
Expand All @@ -69,7 +70,7 @@ public ExpressionNode visitComparisonExpression(ExpressionParser.ComparisonExpre
return left;
}
}
return null;
throw new EvaluationException("Invalid comparison expression: " + ctx.getText());
}

@Override
Expand All @@ -85,7 +86,7 @@ public ExpressionNode visitLogicalExpression(ExpressionParser.LogicalExpressionC
}
return left;
}
return null;
throw new EvaluationException("Invalid logical expression: " + ctx.getText());
}

@Override
Expand All @@ -105,7 +106,7 @@ public ExpressionNode visitArithmeticExpression(ExpressionParser.ArithmeticExpre
return left;
}
}
return null;
throw new EvaluationException("Invalid arithmetic expression: " + ctx.getText());
}

@Override
Expand All @@ -125,7 +126,7 @@ public ExpressionNode visitTerm(ExpressionParser.TermContext ctx) {
return left;
}
}
return null;
throw new EvaluationException("Invalid term: " + ctx.getText());
}

@Override
Expand All @@ -152,7 +153,7 @@ public ExpressionNode visitFactor(ExpressionParser.FactorContext ctx) {
} else if (ctx.parameterAccess() != null) {
return visit(ctx.parameterAccess());
}
return null;
throw new EvaluationException("Invalid factor: " + ctx.getText());
}

@Override
Expand Down Expand Up @@ -285,7 +286,7 @@ public ExpressionNode visitFunctionCall(ExpressionParser.FunctionCallContext ctx
}
}
}
return null;
throw new EvaluationException("Invalid function call: " + ctx.getText());
}

@Override
Expand All @@ -304,7 +305,7 @@ public ExpressionNode visitLiteral(ExpressionParser.LiteralContext ctx) {
} else if (ctx.NULL_LITERAL() != null) {
return new LiteralNode(ctx.NULL_LITERAL().getText(), LiteralNode.Type.NULL);
}
return null;
throw new EvaluationException("Invalid literal: " + ctx.getText());
}

@Override
Expand All @@ -319,7 +320,7 @@ public ExpressionNode visitArrayLiteral(ExpressionParser.ArrayLiteralContext ctx
}
return new LiteralNode(parameterList, LiteralNode.Type.ARRAY);
}
return null;
throw new EvaluationException("Invalid array literal: " + ctx.getText());
}

@Override
Expand Down Expand Up @@ -383,9 +384,7 @@ public ExpressionNode visitArrayIndex(ExpressionParser.ArrayIndexContext ctx) {
} else if (ctx.STRING_LITERAL() != null) {
return new LiteralNode(ctx.STRING_LITERAL().getText(), LiteralNode.Type.STRING);
} else if (ctx.expression() != null) {
if (ctx.expression().size() == 1) {
return visit(ctx.expression(0));
}
return visit(ctx.expression());
} else if (ctx.ASTERISK() != null) {
return null;
} else if (ctx.multipleArrayIndices() != null) {
Expand All @@ -410,7 +409,7 @@ public ExpressionNode visitMultipleArrayIndices(ExpressionParser.MultipleArrayIn
}
return new ArrayIndexNode(expressionNodes, ',');
}
return null;
throw new EvaluationException("Invalid multiple array indices: " + ctx.getText());
}

@Override
Expand All @@ -431,7 +430,7 @@ public ExpressionNode visitSliceArrayIndex(ExpressionParser.SliceArrayIndexConte
}
return new ArrayIndexNode(expressionNodes, ':');
}
return null;
throw new EvaluationException("Invalid slice array index: " + ctx.getText());
}

@Override
Expand All @@ -446,7 +445,7 @@ public ExpressionNode visitSignedExpressions(ExpressionParser.SignedExpressionsC
return visit(ctx.expression());
}
}
return null;
throw new EvaluationException("Invalid signed expressions: " + ctx.getText());
}

@Override
Expand Down Expand Up @@ -485,7 +484,7 @@ public ExpressionNode visitFilterComponent(ExpressionParser.FilterComponentConte
} else if (ctx.parameterAccess() != null) {
return visit(ctx.parameterAccess());
}
return null;
throw new EvaluationException("Invalid filter component: " + ctx.getText());
}

@Override
Expand All @@ -497,7 +496,7 @@ public ExpressionNode visitHeaderAccess(ExpressionParser.HeaderAccessContext ctx
return new HeadersAndPropertiesAccessNode(visit(ctx.propertyName()),
HeadersAndPropertiesAccessNode.Type.HEADER);
}
return null;
throw new EvaluationException("Invalid header access: " + ctx.getText());
}

@Override
Expand All @@ -510,7 +509,7 @@ public ExpressionNode visitPropertyName(ExpressionParser.PropertyNameContext ctx
} else if (ctx.STRING_LITERAL() != null) {
return new LiteralNode(ctx.STRING_LITERAL().getText(), LiteralNode.Type.STRING);
}
return null;
throw new EvaluationException("Invalid property name: " + ctx.getText());
}

@Override
Expand All @@ -522,7 +521,7 @@ public ExpressionNode visitConfigAccess(ExpressionParser.ConfigAccessContext ctx
return new HeadersAndPropertiesAccessNode(visit(ctx.propertyName()),
HeadersAndPropertiesAccessNode.Type.CONFIG);
}
return null;
throw new EvaluationException("Invalid config access: " + ctx.getText());
}

@Override
Expand All @@ -541,7 +540,7 @@ public ExpressionNode visitPropertyAccess(ExpressionParser.PropertyAccessContext
}
}
}
return null;
throw new EvaluationException("Invalid property access: " + ctx.getText());
}

@Override
Expand All @@ -564,7 +563,7 @@ public ExpressionNode visitParameterAccess(ExpressionParser.ParameterAccessConte
}
}
}
return null;
throw new EvaluationException("Invalid parameter access: " + ctx.getText());
}

@Override
Expand All @@ -580,7 +579,7 @@ public ExpressionNode visitConditionalExpression(ExpressionParser.ConditionalExp
if (condition != null && expList.size() == 2) {
return new ConditionalExpressionNode(condition, visit(expList.get(0)), visit(expList.get(1)));
}
return null;
throw new EvaluationException("Invalid conditional expression: " + ctx.getText());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@
*/
package org.apache.synapse.util.synapse.expression;

import org.apache.synapse.util.synapse.expression.exception.SyntaxError;
import org.apache.synapse.util.synapse.expression.exception.EvaluationException;
import org.apache.synapse.util.synapse.expression.exception.SyntaxErrorListener;
import org.junit.Assert;
import org.junit.Test;

import java.util.List;

/**
* Test class for syntax errors in the Synapse Expressions.
*/
Expand All @@ -37,23 +35,12 @@ public void testValidExpressions() {
Assert.assertFalse(syntaxErrorListener.hasErrors());
}

@Test
@Test (expected = EvaluationException.class)
public void testOperationError() {
TestUtils.evaluateWithErrorListener(syntaxErrorListener,"5 >> 3");
Assert.assertTrue("Should throw an error", syntaxErrorListener.hasErrors());

List<SyntaxError> errors = syntaxErrorListener.getErrors();
Assert.assertEquals(1, errors.size());

// Assert details of the error
SyntaxError error = errors.get(0);
Assert.assertTrue(error.getMessage().contains("no viable alternative at input '5>>'"));
Assert.assertEquals(3, error.getCharPositionInLine());

syntaxErrorListener.clearErrors();
}

@Test (expected = RuntimeException.class)
@Test (expected = EvaluationException.class)
public void testInvalidExpressionByLength() {
TestUtils.evaluateExpression("payload.products[0].stock + abc");
}
Expand Down

0 comments on commit cdfdf23

Please sign in to comment.