Skip to content

Commit

Permalink
finally able to replicate #1558
Browse files Browse the repository at this point in the history
work in progress and greatly improved error logging for config js failures
  • Loading branch information
ptrthomas committed May 23, 2021
1 parent 1baef70 commit 38c4802
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1256,7 +1256,7 @@ private JsValue executeJsValue(Value function, Object... args) {
return JS.execute(function, args);
} catch (Exception e) {
String jsSource = function.getSourceLocation().getCharacters().toString();
KarateException ke = JsEngine.fromJsEvalException(jsSource, e);
KarateException ke = JsEngine.fromJsEvalException(jsSource, e, null);
setFailedReason(ke);
throw ke;
}
Expand All @@ -1266,7 +1266,7 @@ public Variable evalJs(String js) {
try {
return new Variable(JS.eval(js));
} catch (Exception e) {
KarateException ke = JsEngine.fromJsEvalException(js, e);
KarateException ke = JsEngine.fromJsEvalException(js, e, null);
setFailedReason(ke);
throw ke;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.intuit.karate.RuntimeHook;
import com.intuit.karate.ScenarioActions;
import com.intuit.karate.debug.DebugThread;
import com.intuit.karate.graal.JsEngine;
import com.intuit.karate.http.ResourceType;
import com.intuit.karate.shell.StringLogAppender;

Expand Down Expand Up @@ -293,8 +294,8 @@ private void evalConfigJs(String js, String displayName) {
Map<String, Object> map = engine.getOrEvalAsMap(fun);
engine.setVariables(map);
} catch (Exception e) {
String message = scenario.getDebugInfo() + "\n" + displayName + "\n" + e.getMessage();
error = new KarateException(message, e);
String message = ">> " + scenario.getDebugInfo() + "\n>> " + displayName + " failed\n>> " + e.getMessage();
error = JsEngine.fromJsEvalException(js, e, message);
stopped = true;
configFailed = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,14 @@ public Value evalWith(Set<String> names, Function<String, Object> getVariable, S
return function.execute(JsValue.fromJava(arg));
}

public static KarateException fromJsEvalException(String js, Exception e) {
public static KarateException fromJsEvalException(String js, Exception e, String message) {
// do our best to make js error traces informative, else thrown exception seems to
// get swallowed by the java reflection based method invoke flow
StackTraceElement[] stack = e.getStackTrace();
StringBuilder sb = new StringBuilder();
if (message != null) {
sb.append(message).append('\n');
}
sb.append(">>>> js failed:\n");
List<String> lines = StringUtils.toStringLines(js);
int index = 0;
Expand All @@ -221,7 +224,7 @@ public static KarateException fromJsEvalException(String js, Exception e) {
if (line.startsWith("<js>") || i > 5) {
break;
}
}
}
return new KarateException(sb.toString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public JsValue evalGlobal(String src) {
try {
return jsEngine.eval(src);
} catch (Exception e) {
throw JsEngine.fromJsEvalException(src, e);
throw JsEngine.fromJsEvalException(src, e, null);
}
}

Expand All @@ -96,7 +96,7 @@ public JsValue evalLocal(String src, boolean returnValue) {
Value value = jsEngine.evalWith(getVariableNames(), this::getVariable, src, returnValue);
return new JsValue(value);
} catch (Exception e) {
throw JsEngine.fromJsEvalException(src, e);
throw JsEngine.fromJsEvalException(src, e, null);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.intuit.karate.core.parajava;

import com.intuit.karate.Results;
import com.intuit.karate.Runner;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class ParallelJavaTest {

// @Test
public void testParallel() {
Results results = Runner.path("classpath:com/intuit/karate/core/parajava/parallel-java.feature")
.configDir("classpath:com/intuit/karate/core/parajava")
.parallel(5);
assertEquals(0, results.getFailCount(), results.getErrorMessages());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function karateConfig() {
return karate.callSingle('setup.feature');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Feature:

Scenario: test 1
* match Hello.sayHello("foo") == "hello foo"

Scenario: test 2
* match Hello.sayHello("foo") == "hello foo"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@ignore
Feature:

Scenario:
* def Hello = Java.type('com.intuit.karate.core.parallel.Hello')

0 comments on commit 38c4802

Please sign in to comment.