{"record":{"id":"33e559c67437d59c","repo":"karatelabs/karate","slug":"js-exception-description","errorCode":null,"errorMessage":"JS exception: {description}","messagePattern":"JS exception: (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/driver/cdp/CdpDriver.java","lineNumber":2021,"sourceCode":"    }\n\n    private Object extractJsValue(CdpResponse response, String expression) {\n        if (response.isError()) {\n            String message = response.getErrorMessage();\n            if (message != null && message.contains(\"Object reference chain is too long\")) {\n                logger.debug(\"script result not serializable, returning null: {}\", truncate(expression, 100));\n                return null;\n            }\n            logger.warn(\"JS error for expression: {}\", truncate(expression, 100));\n            throw new RuntimeException(\"JS error: \" + response.getError());\n        }\n        Object exceptionDetails = response.getResult(\"exceptionDetails\");\n        if (exceptionDetails != null) {\n            // Try to get detailed error message from exception object\n            String description = response.getResultAsString(\"exceptionDetails.exception.description\");\n            if (description != null && !description.isEmpty()) {\n                logger.warn(\"JS exception for: {} -> {}\", truncate(expression, 100), truncate(description, 200));\n                throw new RuntimeException(\"JS exception: \" + description);\n            }\n            // Fall back to text (usually just \"Uncaught\")\n            String text = response.getResultAsString(\"exceptionDetails.text\");\n            logger.warn(\"JS exception for: {} -> {}\", truncate(expression, 100), text);\n            throw new RuntimeException(\"JS exception: \" + text);\n        }\n        Object value = response.getResult(\"result.value\");\n        logger.trace(\"script result: {}\", value);\n        return value;\n    }\n\n    // ========== Screenshot ==========\n\n    /**\n     * Take screenshot and return PNG bytes.\n     */\n    public byte[] screenshot() {\n        return screenshot(false);","sourceCodeStart":2003,"sourceCodeEnd":2039,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/driver/cdp/CdpDriver.java#L2003-L2039","documentation":"Runtime.evaluate reported exceptionDetails containing an exception object whose description is available. Karate throws 'JS exception: <description>' carrying the page-side error (e.g. 'TypeError: x is not a function'). This means your in-page JavaScript threw, not a protocol failure.","triggerScenarios":"driver.script(expression) where the evaluated expression throws at runtime in the page — undefined variables, calling non-existent functions, invalid syntax caught at evaluation, promises rejected without returnByValue-safe handling.","commonSituations":"Typos in the in-page API; running a script before the page defines expected globals; expression relying on DOM not yet present; browser-version-specific JS APIs missing in the test browser.","solutions":["Fix the JS expression — read the description for the exact TypeError/ReferenceError and line info","Guard the expression: check the target exists before invoking (typeof fn === 'function')","Wrap risky evaluation in try/catch inside the JS and return an error object you can assert on","Ensure the page has loaded/initialized before evaluating (use waitUntil/waitFor before script)"],"exampleFix":"// before\nObject v = driver.script(\"window.myApp.getData()\"); // TypeError if myApp undefined\n// after\nObject v = driver.script(\"window.myApp ? window.myApp.getData() : null\");","handlingStrategy":"try-catch","validationCode":"// existence-check inside the expression before invoking\nString guarded = \"(typeof myApp !== 'undefined') ? myApp.getData() : null\";","typeGuard":null,"tryCatchPattern":"try { Object v = driver.script(expr); } catch (RuntimeException e) { if (e.getMessage().startsWith(\"JS exception:\")) { // read description, fix expression or return fallback\n  return null; } throw e; }","preventionTips":["Guard in-page globals with typeof checks before calling them","Ensure the page finished initializing before evaluating app APIs","Test expressions in the browser console first","Keep test-browser versions close to dev-browser versions"],"tags":["cdp","javascript","uncaught-exception"],"backgroundTag":"api-error-response","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"}