{"record":{"id":"99fa0b55d5066989","repo":"karatelabs/karate","slug":"pe-getmessage","errorCode":null,"errorMessage":"${pe.getMessage()}","messagePattern":"\\$\\{pe\\.getMessage\\(\\)\\}","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/ContextRoot.java","lineNumber":346,"sourceCode":"            };\n            case \"decodeURI\" -> (JsInvokable) args ->\n                    URLDecoder.decode(Terms.toStringCoerce(args[0], null), StandardCharsets.UTF_8);\n            case \"undefined\" -> Terms.UNDEFINED;\n            case \"eval\" -> (JsInvokable) args -> {\n                if (args.length == 0) return Terms.UNDEFINED;\n                Object src = args[0];\n                // per ES spec: non-string arguments are returned unchanged\n                if (!(src instanceof String s)) {\n                    return src;\n                }\n                // indirect-eval semantics: evaluate in the global (root) scope\n                try {\n                    return engine.evalRaw(s);\n                } catch (ParserException pe) {\n                    // per spec, eval-code that fails to parse throws a JS\n                    // SyntaxError the caller can catch — a bare ParserException\n                    // would escape as a Java exception no JS catch can see\n                    throw JsErrorException.syntaxError(pe.getMessage());\n                }\n            };\n            case \"Array\", \"Date\", \"Function\", \"Error\", \"Map\", \"Number\", \"BigInt\", \"Boolean\",\n                 \"Object\", \"Promise\", \"RegExp\", \"Set\", \"String\", \"TypeError\", \"ReferenceError\", \"RangeError\",\n                 \"SyntaxError\", \"URIError\", \"EvalError\", \"AggregateError\",\n                 \"WeakMap\", \"WeakSet\" -> builtinConstructor(key);\n            // the top-level `this` object, under its spec name\n            case \"globalThis\" -> thisObject;\n            case \"setTimeout\" -> new JsBuiltinMethod(\"setTimeout\", 2, AsyncSupport::setTimeout);\n            case \"clearTimeout\" -> new JsBuiltinMethod(\"clearTimeout\", 1, AsyncSupport::clearTimeout);\n            case \"Infinity\" -> Double.POSITIVE_INFINITY;\n            case \"Java\" -> new JsJava(bridge);\n            case \"JSON\" -> new JsJson();\n            case \"Math\" -> new JsMath();\n            case \"NaN\" -> Double.NaN;\n            case \"performance\" -> new JsPerformance(nanoOrigin, timeOrigin);\n            case \"structuredClone\" -> new JsBuiltinMethod(\"structuredClone\", 1, JsStructuredClone::call);\n            case \"TextDecoder\" -> new JsTextDecoder();","sourceCodeStart":328,"sourceCodeEnd":364,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/ContextRoot.java#L328-L364","documentation":"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.","triggerScenarios":"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).","commonSituations":"Dynamically built JS strings with interpolation errors; code copied from environments with different syntax support; template concatenation producing malformed expressions.","solutions":["Fix the syntax error reported in the message by inspecting the eval string at runtime.","Log/print the exact string passed to eval before evaluating.","Validate generated JS with a parser or linter before eval.","Wrap eval calls in JS try/catch if dynamic code may legitimately fail to parse."],"exampleFix":"// before\nvar r = eval(\"({a: 1,\");\n// after\ntry { var r = eval(userExpression); } catch (e) { /* SyntaxError handling */ }","handlingStrategy":"try-catch","validationCode":"// validate dynamic JS before eval\nnew Function(userExpression); // throws on syntax errors without executing","typeGuard":null,"tryCatchPattern":"try { var r = eval(code); } catch (e) { if (e instanceof SyntaxError) { log('parse failed: ' + e.message + ' code=' + code); } else throw e; }","preventionTips":["Log the exact string passed to eval before evaluating.","Use a linter/parser to validate generated JS.","Avoid building JS via naive string concatenation."],"tags":["javascript","syntax","eval"],"backgroundTag":"js-syntax-error","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}