{"record":{"id":"b8a28c4ca889e162","repo":"karatelabs/karate","slug":"parserexception-getmessage","errorCode":null,"errorMessage":"{parserException.getMessage()}","messagePattern":"\\{parserException\\.getMessage\\(\\)\\}","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsFunctionConstructor.java","lineNumber":72,"sourceCode":"        } else {\n            for (int i = 0; i < args.length - 1; i++) {\n                if (i > 0) src.append(',');\n                src.append(argToString(args[i]));\n            }\n            src.append(\"\\n) {\\n\");\n            src.append(argToString(args[args.length - 1]));\n            src.append(\"\\n})\");\n        }\n        Engine engine = context.getEngine();\n        if (engine == null) {\n            throw JsErrorException.typeError(\"Function constructor unavailable: no engine\");\n        }\n        try {\n            return engine.evalRaw(src.toString());\n        } catch (io.karatelabs.parser.ParserException e) {\n            // CreateDynamicFunction (§20.2.1.1): an unparsable body is a JS\n            // SyntaxError, not a host parse exception\n            throw JsErrorException.syntaxError(e.getMessage());\n        }\n    }\n\n    private static String argToString(Object arg) {\n        if (arg == null || arg == Terms.UNDEFINED) return \"\";\n        return arg.toString();\n    }\n\n}\n","sourceCodeStart":54,"sourceCodeEnd":82,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsFunctionConstructor.java#L54-L82","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Validate/fix the body string syntax before passing it to new Function.","Log or inspect the parser message in the error to locate the offending token.","Prefer real functions over dynamically compiled strings; if input is user-supplied, validate it or wrap compilation in try/catch for SyntaxError."],"exampleFix":"// before\nconst f = new Function('x', 'return ' + expr); // SyntaxError if expr = 'x ++*'\n// after\nlet f;\ntry { f = new Function('x', 'return ' + expr); }\ncatch (e) { if (e instanceof SyntaxError) f = null; else throw e; }","handlingStrategy":"try-catch","validationCode":"let probe; try { probe = new Function('x', body); } catch (e) { probe = null; } if (!probe) throw new Error('invalid function body: ' + body);","typeGuard":"function compilesToFunction(args, body) { try { new Function(args, body); return true; } catch (e) { return e instanceof SyntaxError ? false : true; } }","tryCatchPattern":"try { return new Function(args, body); } catch (e) { if (e instanceof SyntaxError) throw new Error('invalid generated function: ' + e.message); throw e; }","preventionTips":["Never concatenate unvalidated user input into function bodies.","Test generated bodies with a compile probe before production use.","Log the SyntaxError message verbatim to locate the bad token.","Prefer passing data as arguments rather than interpolating into code."],"tags":["javascript","syntax-error","function-constructor","parser"],"backgroundTag":"json-parse-error","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}