Skip to content
Merged
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
6 changes: 5 additions & 1 deletion graphwalker-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
<groupId>org.graalvm.truffle</groupId>
<artifactId>truffle-api</artifactId>
</dependency>
</dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* #L%
*/

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.graalvm.polyglot.Value;
import org.graphwalker.core.algorithm.Algorithm;
import org.graphwalker.core.generator.PathGenerator;
Expand Down Expand Up @@ -278,8 +279,10 @@ public void execute(Element element) {
// ignore, method is not defined in the execution context
} catch (Throwable t) {
executionStatus = ExecutionStatus.FAILED;
LOG.error(t.getMessage());
throw new MachineException(this, t);
LOG.error(ExceptionUtils.getRootCauseMessage(t));
// Do not obscure the root cause as it's not useful for the end user
// i.e: always showing java.lang.reflect.InvocationTargetException no matter what.
throw new MachineException(this, ExceptionUtils.getRootCause(t));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public TestExecutionException(Result result) {
this.result = result;
}

public TestExecutionException(Result result, Throwable t) {
super(t);
this.result = result;
}

public TestExecutionException(String message) {
super(message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* #L%
*/

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.graphwalker.core.event.EventType;
import org.graphwalker.core.event.Observer;
import org.graphwalker.core.machine.Context;
Expand Down Expand Up @@ -242,18 +243,20 @@ public Result execute() {
public Result execute(boolean ignoreErrors) {
result = new Result();
executeAnnotation(BeforeExecution.class, machine);
Throwable executionException = null;
try {
while (machine.hasNextStep()) {
machine.getNextStep();
}
} catch (MachineException e) {
logger.error(e.getMessage());
failures.put(e.getContext(), e);
executionException = e;
}
executeAnnotation(AfterExecution.class, machine);
result.updateResults(machine, failures);
if (!ignoreErrors && !failures.isEmpty()) {
throw new TestExecutionException(result);
throw new TestExecutionException(result, ExceptionUtils.getRootCause(executionException));
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.graphwalker.core.statistics.Execution;
import org.graphwalker.io.factory.json.JsonContextFactory;
import org.graphwalker.java.annotation.GraphWalker;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Test;
Expand Down Expand Up @@ -184,6 +185,12 @@ public ThrowExceptionTest() {
}

public void throwException() {
PossiblyAnActualRealWorldScenario.problematicMethod();
}
}

public static class PossiblyAnActualRealWorldScenario {
public static void problematicMethod() {
throw new RuntimeException();
}
}
Expand All @@ -194,6 +201,45 @@ public void throwExceptionExecutor() throws IOException {
executor.execute();
}

/**
* Do not obscure the root cause failure
* @throws IOException
*/
@Test
public void doNotObscureRootCauseFailureResult() throws IOException {
Executor executor = new TestExecutor(ThrowExceptionTest.class);

Result result = executor.execute(true);
Assert.assertTrue(result.hasErrors());
JSONArray failures = (JSONArray) result.getResults().get("failures");
ArrayList<String> failureList = new ArrayList<>();
failures.forEach( failure -> {
boolean hasFailureItem = ((JSONObject) failure).has("failure");
if (hasFailureItem) {
failureList.add(((JSONObject) failure).getString("failure"));
}
} );

Assert.assertTrue(failureList.get(0).startsWith("java.lang.RuntimeException"));
}

@Test
public void doNotObscureRootCauseStack() throws IOException {
Executor executor = new TestExecutor(ThrowExceptionTest.class);
Throwable t = null;
try {
executor.execute();
} catch (Throwable thrown) {
t = thrown;
}

Assert.assertNotNull(t);
Assert.assertTrue(Arrays.stream(t.getCause().getStackTrace()).anyMatch( stackTraceElement -> {
System.out.println(stackTraceElement.getMethodName());
return stackTraceElement.getMethodName() == "problematicMethod";
}));
}

@Test
public void multilpeContexts() throws IOException {
List<Context> contexts = new JsonContextFactory().create(Paths.get("org/graphwalker/java/test/PetClinic.json"));
Expand Down