{"record":{"id":"1aff2499990519a7","repo":"karatelabs/karate","slug":"js-error-message-from-context","errorCode":null,"errorMessage":"<js error message from context>","messagePattern":"<js error message from context>","errorType":"exception","errorClass":"EngineException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/Interpreter.java","lineNumber":2512,"sourceCode":"        // convention so host code that loads a script consisting of just a\n        // function definition can use the eval result directly.\n        Object progResult = null;\n        boolean anyNonDecl = false;\n        Object lastFnDecl = null;\n        for (int i = 0, n = node.size(); i < n; i++) {\n            Node child = node.get(i);\n            if (child.isEof()) {\n                break;\n            }\n            if (isFunctionDeclarationStatement(child)) {\n                String fnName = child.getFirst().get(1).getText();\n                lastFnDecl = context.get(fnName);\n                continue;\n            }\n            progResult = eval(child, context);\n            anyNonDecl = true;\n            if (context.isError()) {\n                throw errorAsException(context, child);\n            }\n        }\n        return anyNonDecl ? progResult : lastFnDecl;\n    }\n\n    /**\n     * Convert a context left in JS-error state ({@link CoreContext#isError()}) into the host-facing\n     * {@link EngineException} — the single conversion that makes an uncaught JS {@code throw} look\n     * JS-native to a Java caller (structured {@code jsErrorName} + {@code jsMessage}). Applied at the\n     * top-level statement boundary ({@link #evalProgram}) and at the host-invocation boundary\n     * ({@link JsFunctionNode#call} when a host calls a JS function directly, with no JS caller context),\n     * so {@code someFn.call(null, args)} surfaces an uncaught throw exactly like {@code engine.eval}.\n     *\n     * @param node frames the host-facing (logging) message; the structured {@code jsErrorName}/\n     *             {@code jsMessage} are framing-independent\n     */\n    static EngineException errorAsException(CoreContext context, Node node) {\n        Object errorThrown = context.getErrorThrown();","sourceCodeStart":2494,"sourceCodeEnd":2530,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/Interpreter.java#L2494-L2530","documentation":"The JS interpreter finished (or unwound) a program while the evaluation context carried an error state; it converts that pending error into a thrown exception via errorAsException(context, child). The message is whatever the underlying JS error produced, so this is the generic surfacing point for uncaught JS errors during program evaluation.","triggerScenarios":"Executing a JS program where a statement throws (runtime TypeError, ReferenceError, or a thrown user error) at top-level statement scope in evalProgram; context.isError() is true after evaluating a child statement.","commonSituations":"Calling an undefined function in a JS expression block; throwing custom errors from test scripts; type errors in match/eval-driven JS during scenario runs.","solutions":["Read the embedded `<js error message>` to identify the original JS failure (undefined variable, TypeError, thrown value).","Wrap risky JS in try/catch inside the script itself.","Fix the referenced line/symbol in the JS snippet or feature step that produced the error."],"exampleFix":"// before (JS)\nvar x = notDefined + 1;\n\n// after (JS)\nvar x = (typeof notDefined !== 'undefined' ? notDefined : 0) + 1;","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// inside JS snippets\ntry {\n  var result = riskyCall();\n} catch (e) {\n  console.log('js failed: ' + e);\n  result = null;\n}","preventionTips":["Wrap top-level JS statements that call uncertain APIs in try/catch.","Check variable/function names for typos before running scenarios.","Read the embedded JS error message for the exact line/symbol."],"tags":["javascript","interpreter","runtime-error"],"backgroundTag":"script-runtime-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"}