Java method step failures hide the real error behind `InvocationTargetException`
Summary
When a custom Java Executor(or Answerer) method (a step with a url such as org.example.MyExecutor) throws an exception, the error log, the step's report message and the JUnit failure message only say Invocation failed for method X in class Y. The executor's own error message is lost.
Users have to search the console stack trace for the Caused by: line to find out what went wrong.
Problem
JavaMethodExecutorImpl.executeWithParams calls the executor through reflection (method.invoke). Any exception the executor throws arrives wrapped in a java.lang.reflect.InvocationTargetException, which has no message of its own. The generic catch block logs and rethrows it as is:
} catch (Exception e) {
String errMsg = format("Java exec(): Invocation failed for method %s in class %s", methodName, qualifiedClassName);
LOGGER.error(errMsg + ". Exception - " + e);
throw new RuntimeException(errMsg, e);
}
For example, if MyExecutor.upload() throws IllegalArgumentException("Could not get OAuth2 token: ..."), users see:
| Where | What users see today |
|---|---|
ERROR log (JavaMethodExecutorImpl) |
Java exec(): Invocation failed for method upload in class org.example.MyExecutor. Exception - java.lang.reflect.InvocationTargetException |
Step exception message in the report (ZeroCodeMultiStepsScenarioRunnerImpl, .exceptionMessage(ex.getMessage())) |
java.lang.RuntimeException: Java exec(): Invocation failed for method upload in class org.example.MyExecutor |
| JUnit failure message | ZeroCode Step execution failed. Details:java.lang.RuntimeException: Java exec(): Invocation failed for method upload in class org.example.MyExecutor |
Could not get OAuth2 token: ... doesn't appears in any of them. It is only in the full stack trace that the runner prints to the console, deep in the output.
The workaround today is for every executor/answer-engine author to catch, log and printStackTrace() in their own code. That duplicates the trace, and it is easy to forget.
Expected behaviour
The message of the exception the executor actually threw should appear in the ERROR log, the report and the JUnit failure message, e.g.:
Java exec(): Invocation failed for method upload in class org.example.MyExecutor. Cause: java.lang.IllegalArgumentException: Could not get OAuth2 token: ...
Proposed fix
Catch InvocationTargetException separately in JavaMethodExecutorImpl.executeWithParams:
- add the target exception to the message;
- log the target exception's full stack trace through the logger.
The message prefix and the exception cause chain stay the same, so existing behaviour and tests are unaffected. It is a small change in one file plus a unit test coverage.
Hi, I’d like to work on this. Can I take it?
Hello @CaioSavoldi, thanks for your interest.
As this was a small change, looks a PR already pending by the new author.
is it possible for you review this PR please and add any more unit tests(where needed) ?
Also is it possible to test the feature manually for you and confirm the feature works as expected?
Hi, I'd like to help validate the existing fix in #809 rather than duplicate it. I can reproduce the exception-reporting problem, test the PR's behavior, and check whether the underlying executor exception appears in the log, report, and JUnit failure message. If I find missing test coverage, I'll coordinate here before proposing additional tests. Would this testing/review contribution still be useful?
Hi, ...Would this testing/review contribution still be useful?
Yes, that'll be really useful @Orcus01 . One or two scenarios with the usage and example output will definitely add value to the above PR.
you can paste the test results here(in this ticket) when you're ready. Thanks in advance.
I compared #809 (debef826) with its base (3f91834d) on Java 17.
-
Executor regression test.
mvn -pl core -Dtest=JavaMethodExecutorImplTest testpassed all 9 tests on the PR. -
JUnit runner. I ran the existing
unit_test_files/java_apis/03_test_json_java_service_method_Exception.jsonfixture using@RunWith(ZeroCodeUnitRunner.class)and@JsonTestCase. The step callsAddService.squareRootwith-255.0. The run failed as expected on both revisions, with these differences in the JUnit error message:- Base:
Java exec(): Invocation failed for method squareRoot in class org.jsmart.zerocode.core.AddService - PR:
Java exec(): Invocation failed for method squareRoot in class org.jsmart.zerocode.core.AddService. Cause: java.lang.RuntimeException: Can not square root a negative number
- Base:
The executor log on the PR also includes the underlying exception. I verified the JUnit/Surefire output and executor log; I did not confirm that the message is surfaced in the standalone Zerocode report.
PR: Java exec(): Invocation failed for method squareRoot in class org.jsmart.zerocode.core.AddService. Cause: java.lang.RuntimeException: Can not square root a negative number
This is great. That shows PR fix is good.
...I did not confirm that the message is surfaced in the standalone Zerocode report.
ok. Do you have mechanism to check this?
No.2 is not a blocker as long as it works. There were no changes wrt reports feature, but worth checking.
I checked the standalone JSON, CSV, and HTML reports using the same negative square-root scenario on the base commit and #809. All three mark the step as FAILED on both revisions. The underlying exception message appears in the executor log and JUnit failure output on #809, but it is absent from the standalone reports on both revisions.
I checked the standalone JSON, CSV, and HTML reports using the same negative square-root scenario on the base commit and #809. All three mark the step as
FAILEDon both revisions. The underlying exception message appears in the executor log and JUnit failure output on #809, but it is absent from the standalone reports on both revisions.
Ah! that needs a fix then 🔴 .
Would you mind raising a new Issue(BUG) with the examples/tests you used above and attach the report as evidence which is missing the failure description.