Skip to content

Original exception wrapped in IllegalArgumentException/InvocationTargetException in annotated step listeners [BATCH-2213] #1387

@spring-projects-issues

Description

@spring-projects-issues

Mirko Adebahr opened BATCH-2213 and commented

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()
		}
	}
}

Affects: 2.2.2

Referenced from: pull request #584, and commits 61bec28

Backported to: 4.1.0.M1

Metadata

Metadata

Assignees

No one assigned

    Labels

    has: backportsLegacy label from JIRA. Superseded by "for: backport-to-x.x.x"type: bug

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions