[BATCH-2213] Original exception wrapped in IllegalArgumentException/InvocationTargetException in annotated step listeners Created: 16/Apr/14  Updated: 17/Dec/19  Resolved: 02/May/18

Status: Resolved
Project: Spring Batch
Component/s: None
Affects Version/s: 2.2.2
Fix Version/s: 4.1.0, 4.1.0.M1

Type: Bug Priority: Minor
Reporter: Mirko Adebahr Assignee: Mahmoud Ben Hassine
Resolution: Complete Votes: 0
Labels: pull-request-submitted
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Pull Request URL: https://github.com/spring-projects/spring-batch/pull/584

 Description   

If an exception is thrown from a method with a listener annotation like @AfterProcess, the original exception passed to onProcessError() or onSkipInProcess() is wrapped as follows:

StepListenerFailedException -> IllegalArgumentException -> InvocationTargetException -> #getTargetException()

If an implementation of ItemProcessListener is registered (no annotations), the original exception is the direct cause of the StepListenerFailedException as expected.

Expected behaviour is that in both cases the original exception can be obtained by StepListenerFailedException#getCause().

Example:

@AfterProcess
public void afterProcess(final I i, final O o) {
	throw new RuntimeException("Cause");
}

...

@OnProcessError
public void onProcessError(final I i, final Exception e) {
	if (e instanceof StepListenerFailedException) {
		Throwable cause = e.getCause();
		if ((cause instanceof IllegalArgumentException)
				&& (cause.getCause() instanceof InvocationTargetException)) {
			cause =
					((InvocationTargetException) cause.getCause())
							.getTargetException();
			// now cause is the original runtime exception thrown in afterProcess()
		}
	}
}


 Comments   
Comment by Seo Kyung-Seok [ 01/Jul/14 ]

This problem is occurred in SimpleMethodInvoker.invokeMethod(Object...). If a exception was occurred after the method.invoke(target, invokeArgs) was executed In SimpleMethodInvoker.invokeMethod(Object...), the exception(IllegalAccessException, IllegalArgumentException and InvocationTargetException) is wrapped a IllegalArgumentException. So, it is more efficient that using ReflectionUtils.handleReflectionException(e) in the method. In this case, the (@OnProcessError)onProcessError() method is not neccessary.

And I submitted pull request.
https://github.com/spring-projects/spring-batch/pull/332

Comment by Seo Kyung-Seok [ 01/Jul/14 ]

This issue assignee isn't specified yet.

Comment by Seo Kyung-Seok [ 09/Jul/14 ]

I made the pull request in 3.0.x branch.

https://github.com/spring-projects/spring-batch/pull/338

Thanks.

Comment by Michael Minella [ 02/May/18 ]

Merged asĀ 61bec2805dda06b25f1dd5c3030066754cdf7b94.

Comment by Spring Issuemaster [ 17/Dec/19 ]

The Spring Batch Framework has moved from Jira to GitHub Issues. This issue was migrated to spring-projects/spring-batch#1387.

Generated at Sat Jan 25 16:53:19 UTC 2020 using Jira 7.13.8#713008-sha1:1606a5c1e7006e1ab135aac81f7a9566b2dbc3a6.