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

Include root cause when using DataTable.asList and friends #2949

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- [Core] Include root cause when using DataTable.asList and friends ([#2949](https://github.com/cucumber/cucumber-jvm/pull/2949) M.P. Korstanje)

### Changed
- [JUnit Platform Engine] Use JUnit Platform 1.11.3 (JUnit Jupiter 5.11.3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,20 @@ public CucumberInvocationTargetException(Located located, InvocationTargetExcept
this.invocationTargetException = invocationTargetException;
}

/**
* @deprecated use {@link #getCause()} instead.
*/
@Deprecated
public Throwable getInvocationTargetExceptionCause() {
return invocationTargetException.getCause();
return getCause();
}

public Located getLocated() {
return located;
}

@Override
public Throwable getCause() {
return invocationTargetException.getCause();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,72 @@
import io.cucumber.core.backend.CucumberInvocationTargetException;
import io.cucumber.core.backend.Located;

import java.util.function.Consumer;

final class StackManipulation {

private StackManipulation() {

}

static Throwable removeFrameworkFrames(CucumberInvocationTargetException invocationException) {
Throwable error = invocationException.getInvocationTargetExceptionCause();
StackTraceElement[] stackTraceElements = error.getStackTrace();
Located located = invocationException.getLocated();

int newStackTraceLength = findIndexOf(located, stackTraceElements);
if (newStackTraceLength == -1) {
return error;
}
static Throwable removeFrameworkFramesAndAppendStepLocation(
CucumberInvocationTargetException invocationException, StackTraceElement stepLocation
) {
Throwable error = invocationException.getCause();
walkException(error, appendStepLocation(invocationException.getLocated(), stepLocation));
return error;
}

StackTraceElement[] newStackTrace = new StackTraceElement[newStackTraceLength];
System.arraycopy(stackTraceElements, 0, newStackTrace, 0, newStackTraceLength);
error.setStackTrace(newStackTrace);
static Throwable removeFrameworkFrames(CucumberInvocationTargetException invocationException) {
Throwable error = invocationException.getCause();
walkException(invocationException, removeFramesAfter(invocationException.getLocated()));
return error;
}

private static int findIndexOf(Located located, StackTraceElement[] stackTraceElements) {
if (stackTraceElements.length == 0) {
return -1;
private static void walkException(Throwable cause, Consumer<Throwable> action) {
while (cause != null) {
action.accept(cause);
cause = cause.getCause();
}
}

int newStackTraceLength;
for (newStackTraceLength = 1; newStackTraceLength < stackTraceElements.length; ++newStackTraceLength) {
if (located.isDefinedAt(stackTraceElements[newStackTraceLength - 1])) {
break;
static Consumer<Throwable> removeFramesAfter(Located located) {
return throwable -> {
StackTraceElement[] stackTrace = throwable.getStackTrace();
int lastFrame = findIndexOf(located, stackTrace);
if (lastFrame == -1) {
return;
}
}
return newStackTraceLength;
StackTraceElement[] newStackTrace = new StackTraceElement[lastFrame + 1];
System.arraycopy(stackTrace, 0, newStackTrace, 0, lastFrame + 1);
throwable.setStackTrace(newStackTrace);
};
}

static Throwable removeFrameworkFramesAndAppendStepLocation(
CucumberInvocationTargetException invocationException, StackTraceElement stepLocation
) {
Located located = invocationException.getLocated();
Throwable error = invocationException.getInvocationTargetExceptionCause();
if (stepLocation == null) {
return error;
}
StackTraceElement[] stackTraceElements = error.getStackTrace();
int newStackTraceLength = findIndexOf(located, stackTraceElements);
if (newStackTraceLength == -1) {
return error;
}
StackTraceElement[] newStackTrace = new StackTraceElement[newStackTraceLength + 1];
System.arraycopy(stackTraceElements, 0, newStackTrace, 0, newStackTraceLength);
newStackTrace[newStackTraceLength] = stepLocation;
error.setStackTrace(newStackTrace);
return error;
private static Consumer<Throwable> appendStepLocation(Located located, StackTraceElement stepLocation) {
return throwable -> {
if (located == null) {
return;
}
StackTraceElement[] stackTrace = throwable.getStackTrace();
int lastFrame = findIndexOf(located, stackTrace);
if (lastFrame == -1) {
return;
}
// One extra for the step location
StackTraceElement[] newStackTrace = new StackTraceElement[lastFrame + 1 + 1];
System.arraycopy(stackTrace, 0, newStackTrace, 0, lastFrame + 1);
newStackTrace[lastFrame + 1] = stepLocation;
throwable.setStackTrace(newStackTrace);
};
}

private static int findIndexOf(Located located, StackTraceElement[] stackTraceElements) {
for (int index = 0; index < stackTraceElements.length; index++) {
if (located.isDefinedAt(stackTraceElements[index])) {
return index;
}
}
return -1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class StepDefinitionMatchTest {
private final Located stubbedLocation = new Located() {
@Override
public boolean isDefinedAt(StackTraceElement stackTraceElement) {
return false;
return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void can_provide_location_of_step() throws Throwable {
JavaStepDefinition definition = new JavaStepDefinition(method, "three (.*) mice", lookup);
CucumberInvocationTargetException exception = assertThrows(CucumberInvocationTargetException.class,
() -> definition.execute(new Object[0]));
Optional<StackTraceElement> match = stream(exception.getInvocationTargetExceptionCause().getStackTrace())
Optional<StackTraceElement> match = stream(exception.getCause().getStackTrace())
.filter(definition::isDefinedAt).findFirst();
StackTraceElement stackTraceElement = match.get();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void exception_from_step_should_be_defined_at_step_definition_class() {

CucumberInvocationTargetException exception = assertThrows(CucumberInvocationTargetException.class,
() -> stepDefinition.execute(new Object[0]));
assertThat(exception.getInvocationTargetExceptionCause(),
assertThat(exception.getCause(),
new CustomTypeSafeMatcher<Throwable>("exception with matching stack trace") {
@Override
protected boolean matchesSafely(Throwable item) {
Expand Down