Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/com/alibaba/qlexpress4/Express4Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ public QCompileCache parseToDefinitionWithCache(String script) {
return getParseFuture(script).get();
}
catch (Exception e) {
Throwable compileException = e.getCause();
Throwable compileException = e.getCause() != null ? e.getCause() : e;
throw compileException instanceof QLSyntaxException ? (QLSyntaxException)compileException
: new RuntimeException(compileException);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,9 @@ private BiFunction<ErrorReporter, Object, Supplier<Object>> getFieldUnAccessible
public static QLRuntimeException unwrapMethodInvokeEx(ErrorReporter errorReporter, String methodName,
Exception ex) {
if (ex instanceof IllegalArgumentException) {
return errorReporter.reportFormat(QLErrorCodes.INVOKE_METHOD_WITH_WRONG_ARGUMENTS.name(),
String.format(QLErrorCodes.INVOKE_METHOD_WITH_WRONG_ARGUMENTS.getErrorMsg(), methodName));
return errorReporter.reportFormatWithCatch(ex,
QLErrorCodes.INVOKE_METHOD_WITH_WRONG_ARGUMENTS.name(),
QLErrorCodes.INVOKE_METHOD_WITH_WRONG_ARGUMENTS.getErrorMsg(), methodName);
}
else if (ex instanceof InvocationTargetException) {
return errorReporter.report(((InvocationTargetException)ex).getTargetException(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ else if (!(mayBeIterable instanceof Iterable)) {
}
}
catch (UserDefineException e) {
throw errorReporter.reportFormat(QLErrorCodes.FOR_EACH_TYPE_MISMATCH.name(),
throw errorReporter.reportFormatWithCatch(e,
QLErrorCodes.FOR_EACH_TYPE_MISMATCH.name(),
QLErrorCodes.FOR_EACH_TYPE_MISMATCH.getErrorMsg(),
itCls.getName(),
item == null ? "null" : item.getClass().getName());
Expand All @@ -69,7 +70,7 @@ else if (!(mayBeIterable instanceof Iterable)) {
throw (QLRuntimeException)t;
}
// should not run there
throw errorReporter.report(QLErrorCodes.FOR_EACH_UNKNOWN_ERROR.name(),
throw errorReporter.report(t, QLErrorCodes.FOR_EACH_UNKNOWN_ERROR.name(),
QLErrorCodes.FOR_EACH_UNKNOWN_ERROR.getErrorMsg());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ private Object newInstance(QContext qContext) {
QLErrorCodes.INVOKE_CONSTRUCTOR_INNER_ERROR.getErrorMsg());
}
catch (Exception e) {
throw errorReporter.report(QLErrorCodes.INVOKE_CONSTRUCTOR_UNKNOWN_ERROR.name(),
throw errorReporter.report(e,
QLErrorCodes.INVOKE_CONSTRUCTOR_UNKNOWN_ERROR.name(),
QLErrorCodes.INVOKE_CONSTRUCTOR_UNKNOWN_ERROR.getErrorMsg());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ private Object newObject(Constructor<?> constructor, Object[] params) {
QLErrorCodes.INVOKE_CONSTRUCTOR_INNER_ERROR.getErrorMsg());
}
catch (Exception e) {
throw errorReporter.report(QLErrorCodes.INVOKE_CONSTRUCTOR_UNKNOWN_ERROR.name(),
throw errorReporter.report(e,
QLErrorCodes.INVOKE_CONSTRUCTOR_UNKNOWN_ERROR.name(),
QLErrorCodes.INVOKE_CONSTRUCTOR_UNKNOWN_ERROR.getErrorMsg());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public static QLRuntimeException wrapThrowable(Throwable t, ErrorReporter errorR

public static QLRuntimeException reportUserDefinedException(ErrorReporter errorReporter, UserDefineException e) {
if (Objects.equals(e.getType(), UserDefineException.ExceptionType.INVALID_ARGUMENT)) {
throw errorReporter.report(QLErrorCodes.INVALID_ARGUMENT.name(), e.getMessage());
throw errorReporter.report(e, QLErrorCodes.INVALID_ARGUMENT.name(), e.getMessage());
}
else {
throw errorReporter.report(QLErrorCodes.BIZ_EXCEPTION.name(), e.getMessage());
throw errorReporter.report(e, QLErrorCodes.BIZ_EXCEPTION.name(), e.getMessage());
}
}
}
32 changes: 32 additions & 0 deletions src/test/java/com/alibaba/qlexpress4/Express4RunnerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1852,4 +1852,36 @@ public void defaultAndSwitchAsVariable() {
.execute("default = 1\nswitch = 2;\ndefault+switch", Collections.emptyMap(), QLOptions.DEFAULT_OPTIONS);
assertEquals(3, result.getResult());
}

@Test
public void parseToDefinitionWithCacheInvalidScriptTest() {
Express4Runner express4Runner = new Express4Runner(InitOptions.DEFAULT_OPTIONS);
try {
express4Runner.parseToDefinitionWithCache("invalid syntax @@#$");
fail("Expected QLSyntaxException for invalid script");
}
catch (QLSyntaxException e) {
// The exception should have meaningful error information
assertNotNull(e.getMessage());
}
}

@Test
public void methodInvokeWrongArgTypeCausePreservedTest() {
Express4Runner express4Runner =
new Express4Runner(InitOptions.builder().securityStrategy(QLSecurityStrategy.open()).build());
Map<String, Object> context = new HashMap<>();
context.put("str", "hello");
try {
// Call String.charAt with wrong argument type (passing a string instead of int)
express4Runner.execute("str.charAt(\"notAnInt\")", context, QLOptions.DEFAULT_OPTIONS);
fail("Expected QLRuntimeException for wrong argument type");
}
catch (QLRuntimeException e) {
Assert.assertEquals("INVOKE_METHOD_WITH_WRONG_ARGUMENTS", e.getErrorCode());
assertNotNull("Original IllegalArgumentException should be preserved as cause", e.getCause());
assertTrue("Cause should be IllegalArgumentException",
e.getCause() instanceof IllegalArgumentException);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
public class MockErrorReporter implements ErrorReporter {
@Override
public QLRuntimeException reportFormatWithCatch(Object catchObj, String errorCode, String format, Object... args) {
return new QLRuntimeException(null, String.format(format, args), errorCode);
return new QLRuntimeException(catchObj, String.format(format, args), errorCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,19 @@ public void varArgTest() {
mockQContextParent.pushParameter(parentParameters);
newInstruction.execute(mockQContextParent, QLOptions.DEFAULT_OPTIONS);
}

@Test
public void exceptionCausePreservedTest() {
ErrorReporter errorReporter = new MockErrorReporter();
NewInstanceInstruction newInstruction = new NewInstanceInstruction(errorReporter, SomeInter.class, 0);
MockQContextParent mockQContextParent = new MockQContextParent(false);
try {
newInstruction.execute(mockQContextParent, QLOptions.DEFAULT_OPTIONS);
Assert.fail("Expected QLRuntimeException");
}
catch (QLRuntimeException e) {
Assert.assertEquals("INVOKE_CONSTRUCTOR_UNKNOWN_ERROR", e.getErrorCode());
Assert.assertNotNull("Original exception should be preserved as cause", e.getCause());
}
}
}