Skip to content
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

Exceptions thrown from classes executed by the java api are not propagated #688

Closed
javiertuya opened this issue Oct 20, 2024 · 3 comments · Fixed by #690
Closed

Exceptions thrown from classes executed by the java api are not propagated #688

javiertuya opened this issue Oct 20, 2024 · 3 comments · Fixed by #690
Assignees

Comments

@javiertuya
Copy link
Collaborator

When a java api operation throws an exception, the exception message and stacktrace are not shown as part of the scenario stacktrace, making it difficult to debug.

To reproduce:

  • Start from a new branch
  • Edit org.jsmart.zerocode.core.AddService and replace return 30 in the 'anInteger' method by throw new RuntimeException("INTENTIONAL EXCEPTION")
  • Optionally, edit the resource file unit_test_files/java_apis/02_test_json_java_service_method_no_param.json and remove "loop": 5,
  • Run the test in package org.jsmart.zerocode.core.javamethod

Expected: Some information about the RuntimeException INTENTIONAL EXCEPTION, and its stacktrace should be shown
Actual: It is ommited. This is the stacktrace:

2024-10-20 11:55:44,184 [main] ERROR org.jsmart.zerocode.core.engine.executor.javaapi.JavaMethodExecutorImpl - Java exec(): Invocation failed for method anInteger in class org.jsmart.zerocode.core.AddService. Exception - java.lang.reflect.InvocationTargetException
2024-10-20 11:55:44,184 [main] ERROR org.jsmart.zerocode.core.engine.executor.javaapi.JavaMethodExecutorImpl - Exception - java.lang.RuntimeException: Java exec(): Invocation failed for method anInteger in class org.jsmart.zerocode.core.AddService
2024-10-20 11:55:44,185 [main] ERROR org.jsmart.zerocode.core.engine.executor.ApiServiceExecutorImpl - Java method execution exception - java.lang.RuntimeException: java.lang.RuntimeException: Java exec(): Invocation failed for method anInteger in class org.jsmart.zerocode.core.AddService
java.lang.RuntimeException: java.lang.RuntimeException: java.lang.RuntimeException: Java exec(): Invocation failed for method anInteger in class org.jsmart.zerocode.core.AddService
	at org.jsmart.zerocode.core.engine.executor.ApiServiceExecutorImpl.executeJavaOperation(ApiServiceExecutorImpl.java:53)
	at org.jsmart.zerocode.core.runner.ZeroCodeMultiStepsScenarioRunnerImpl.executeApi(ZeroCodeMultiStepsScenarioRunnerImpl.java:471)
	at org.jsmart.zerocode.core.runner.ZeroCodeMultiStepsScenarioRunnerImpl.executeRetry(ZeroCodeMultiStepsScenarioRunnerImpl.java:256)
	at org.jsmart.zerocode.core.runner.ZeroCodeMultiStepsScenarioRunnerImpl.executeRetryWithSteps(ZeroCodeMultiStepsScenarioRunnerImpl.java:195)
	at org.jsmart.zerocode.core.runner.ZeroCodeMultiStepsScenarioRunnerImpl.executeSteps(ZeroCodeMultiStepsScenarioRunnerImpl.java:177)
	at org.jsmart.zerocode.core.runner.ZeroCodeMultiStepsScenarioRunnerImpl.runScenario(ZeroCodeMultiStepsScenarioRunnerImpl.java:140)
(...)
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: Java exec(): Invocation failed for method anInteger in class org.jsmart.zerocode.core.AddService
	at org.jsmart.zerocode.core.engine.executor.javaapi.JavaMethodExecutorImpl.execute(JavaMethodExecutorImpl.java:63)
	at org.jsmart.zerocode.core.engine.executor.ApiServiceExecutorImpl.executeJavaOperation(ApiServiceExecutorImpl.java:50)
	... 22 more
Caused by: java.lang.RuntimeException: Java exec(): Invocation failed for method anInteger in class org.jsmart.zerocode.core.AddService
	at org.jsmart.zerocode.core.engine.executor.javaapi.JavaMethodExecutorImpl.executeWithParams(JavaMethodExecutorImpl.java:114)
	at org.jsmart.zerocode.core.engine.executor.javaapi.JavaMethodExecutorImpl.execute(JavaMethodExecutorImpl.java:47)
	... 23 more
2024-10-20 11:55:44,186 [main] ERROR org.jsmart.zerocode.core.runner.ZeroCodeMultiStepsScenarioRunnerImpl - ###Exception while executing a step in the zerocode dsl.
2024-10-20 11:55:44,188 [main] ERROR org.jsmart.zerocode.core.runner.StepNotificationHandler - Exception occurred while executing Scenario:[Given_When_Then-Flow name For Java Service], --> Step:[javaMethodExecution], Details: java.lang.RuntimeException: ZeroCode Step execution failed. Details:java.lang.RuntimeException: java.lang.RuntimeException: java.lang.RuntimeException: Java exec(): Invocation failed for method anInteger in class org.jsmart.zerocode.core.AddService
2024-10-20 11:55:44,193[main] org.jsmart.zerocode.core.runner.ZeroCodeMultiStepsScenarioRunnerImpl -m
@javiertuya
Copy link
Collaborator Author

I've located the cause: org.jsmart.zerocode.core.engine.executor.javaapi.executeWithParams catches exceptions and rethrows a new RuntimeException with the message only.

If adding both the message and catched exception to the throw statement, the stacktrace is augmented with:

Caused by: java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.jsmart.zerocode.core.engine.executor.javaapi.JavaMethodExecutorImpl.executeWithParams(JavaMethodExecutorImpl.java:107)
	... 24 more
Caused by: java.lang.RuntimeException: INTENTIONAL EXCEPTION
	at org.jsmart.zerocode.core.AddService.anInteger(AddService.java:27)
	... 29 more

I will prepare a PR to fix it. @authorjapps can you please assign this to me?

@authorjapps
Copy link
Owner

authorjapps commented Oct 20, 2024

I've located the cause: org.jsmart.zerocode.core.engine.executor.javaapi.executeWithParams catches exceptions and rethrows a new RuntimeException with the message only.

If adding both the message and catched exception to the throw statement, the stacktrace is augmented with:

Caused by: java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.jsmart.zerocode.core.engine.executor.javaapi.JavaMethodExecutorImpl.executeWithParams(JavaMethodExecutorImpl.java:107)
	... 24 more
Caused by: java.lang.RuntimeException: INTENTIONAL EXCEPTION
	at org.jsmart.zerocode.core.AddService.anInteger(AddService.java:27)
	... 29 more

I will prepare a PR to fix it. @authorjapps can you please assign this to me?

yep, that's right. Sounds amazing. Please go ahead 🚀
Issue assigned to you now.

Also, added you now as collaborator. Click here to accept.
Now, you can,

  • Approve PRs
  • Trigger CI/CD pipelines
  • assign/unassign issues
    etc many more things.

Also, can you add relevant logs as ERROR level for the DB executor, to surface important exception messages etc to the end user? This will be very helpful to Devs and SDETs to avoid rabbit holes when any problem arises.

(Ignore me if you've already thought of doing it anyways, as part of this ticket :) )

@javiertuya
Copy link
Collaborator Author

javiertuya commented Oct 20, 2024

@authorjapps Great!! Thank you.

Also, can you add relevant logs as ERROR level for the DB executor, to surface important exception messages etc to the end user? This will be very helpful to Devs and SDETs to avoid rabbit holes when any problem arises.

I did, but because of this issue, I logged the messages and exceptions, which generates the needed stacktrace to locate failures (e.g. sql syntax, or invalid CSV cells). After fixing this issue, I think that only logging the error messages will be enough. I will check it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants