karatelabs/karate · error · JsErrorException

{parserException.getMessage()}

Error message

{parserException.getMessage()}

What it means

CreateDynamicFunction (spec §20.2.1.1) requires that a syntactically invalid function body or parameter list produce a SyntaxError. Karate catches the host ParserException from engine.evalRaw and rethrows it as a JS SyntaxError whose message is the parser's message.

Solutions

  1. Validate/fix the body string syntax before passing it to new Function.
  2. Log or inspect the parser message in the error to locate the offending token.
  3. Prefer real functions over dynamically compiled strings; if input is user-supplied, validate it or wrap compilation in try/catch for SyntaxError.

Example fix

// before
const f = new Function('x', 'return ' + expr); // SyntaxError if expr = 'x ++*'
// after
let f;
try { f = new Function('x', 'return ' + expr); }
catch (e) { if (e instanceof SyntaxError) f = null; else throw e; }
Defensive patterns

Strategy: try-catch

Validate before calling

let probe; try { probe = new Function('x', body); } catch (e) { probe = null; } if (!probe) throw new Error('invalid function body: ' + body);

Type guard

function compilesToFunction(args, body) { try { new Function(args, body); return true; } catch (e) { return e instanceof SyntaxError ? false : true; } }

Try / catch

try { return new Function(args, body); } catch (e) { if (e instanceof SyntaxError) throw new Error('invalid generated function: ' + e.message); throw e; }

Prevention

When it happens

Trigger: new Function('x', 'return x ++*'), new Function(') {'), or any body/param string that fails the Karate JS parser; also template-built code where interpolated values break syntax.

Common situations: Building function bodies by string concatenation with user input, config-driven expression compilation, minified/generated code strings fed to new Function in an eval-based hook.

Understand the failure class

Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.

Related errors


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

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/js/JsFunctionConstructor.java:72

        } else {
            for (int i = 0; i < args.length - 1; i++) {
                if (i > 0) src.append(',');
                src.append(argToString(args[i]));
            }
            src.append("\n) {\n");
            src.append(argToString(args[args.length - 1]));
            src.append("\n})");
        }
        Engine engine = context.getEngine();
        if (engine == null) {
            throw JsErrorException.typeError("Function constructor unavailable: no engine");
        }
        try {
            return engine.evalRaw(src.toString());
        } catch (io.karatelabs.parser.ParserException e) {
            // CreateDynamicFunction (§20.2.1.1): an unparsable body is a JS
            // SyntaxError, not a host parse exception
            throw JsErrorException.syntaxError(e.getMessage());
        }
    }

    private static String argToString(Object arg) {
        if (arg == null || arg == Terms.UNDEFINED) return "";
        return arg.toString();
    }

}

View on GitHub (pinned to a22eb90246)