karatelabs/karate · error · JsErrorException

${pe.getMessage()}

Error message

${pe.getMessage()}

What it means

When JS eval-code fails to parse, Karate converts the Java ParserException into a JavaScript SyntaxError via JsErrorException.syntaxError(pe.getMessage()). This is done so JS `try/catch` can intercept the failure — a raw ParserException would escape as a Java exception invisible to JS handlers.

Solutions

  1. Fix the syntax error reported in the message by inspecting the eval string at runtime.
  2. Log/print the exact string passed to eval before evaluating.
  3. Validate generated JS with a parser or linter before eval.
  4. Wrap eval calls in JS try/catch if dynamic code may legitimately fail to parse.

Example fix

// before
var r = eval("({a: 1,");
// after
try { var r = eval(userExpression); } catch (e) { /* SyntaxError handling */ }
Defensive patterns

Strategy: try-catch

Validate before calling

// validate dynamic JS before eval
new Function(userExpression); // throws on syntax errors without executing

Try / catch

try { var r = eval(code); } catch (e) { if (e instanceof SyntaxError) { log('parse failed: ' + e.message + ' code=' + code); } else throw e; }

Prevention

When it happens

Trigger: Calling eval('...') or engine.evalRaw on source with a syntax error — unbalanced braces/parens, invalid tokens, unsupported syntax — during global-object initialization (initGlobal's eval binding).

Common situations: Dynamically built JS strings with interpolation errors; code copied from environments with different syntax support; template concatenation producing malformed expressions.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/99fa0b55d5066989. Report an issue: GitHub.

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/js/ContextRoot.java:346

            };
            case "decodeURI" -> (JsInvokable) args ->
                    URLDecoder.decode(Terms.toStringCoerce(args[0], null), StandardCharsets.UTF_8);
            case "undefined" -> Terms.UNDEFINED;
            case "eval" -> (JsInvokable) args -> {
                if (args.length == 0) return Terms.UNDEFINED;
                Object src = args[0];
                // per ES spec: non-string arguments are returned unchanged
                if (!(src instanceof String s)) {
                    return src;
                }
                // indirect-eval semantics: evaluate in the global (root) scope
                try {
                    return engine.evalRaw(s);
                } catch (ParserException pe) {
                    // per spec, eval-code that fails to parse throws a JS
                    // SyntaxError the caller can catch — a bare ParserException
                    // would escape as a Java exception no JS catch can see
                    throw JsErrorException.syntaxError(pe.getMessage());
                }
            };
            case "Array", "Date", "Function", "Error", "Map", "Number", "BigInt", "Boolean",
                 "Object", "Promise", "RegExp", "Set", "String", "TypeError", "ReferenceError", "RangeError",
                 "SyntaxError", "URIError", "EvalError", "AggregateError",
                 "WeakMap", "WeakSet" -> builtinConstructor(key);
            // the top-level `this` object, under its spec name
            case "globalThis" -> thisObject;
            case "setTimeout" -> new JsBuiltinMethod("setTimeout", 2, AsyncSupport::setTimeout);
            case "clearTimeout" -> new JsBuiltinMethod("clearTimeout", 1, AsyncSupport::clearTimeout);
            case "Infinity" -> Double.POSITIVE_INFINITY;
            case "Java" -> new JsJava(bridge);
            case "JSON" -> new JsJson();
            case "Math" -> new JsMath();
            case "NaN" -> Double.NaN;
            case "performance" -> new JsPerformance(nanoOrigin, timeOrigin);
            case "structuredClone" -> new JsBuiltinMethod("structuredClone", 1, JsStructuredClone::call);
            case "TextDecoder" -> new JsTextDecoder();

View on GitHub (pinned to a22eb90246)