{"record":{"id":"a97ef4507c813a44","repo":"karatelabs/karate","slug":"js-exception-text","errorCode":null,"errorMessage":"JS exception: {text}","messagePattern":"JS exception: (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/driver/cdp/CdpDriver.java","lineNumber":2026,"sourceCode":"            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);\n    }\n\n    /**\n     * Take screenshot, optionally embed in report.\n     */","sourceCodeStart":2008,"sourceCodeEnd":2044,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/driver/cdp/CdpDriver.java#L2008-L2044","documentation":"Like the previous case, Runtime.evaluate produced exceptionDetails, but the exception object carried no description, so Karate falls back to exceptionDetails.text — usually just the string 'Uncaught'. The page script threw, but the browser provided no detailed description.","triggerScenarios":"driver.script(expression) with an uncaught error whose exceptionDetails lacks exception.description (common for non-Error thrown values like strings or for some parse errors), leaving only the generic text.","commonSituations":"In-page code doing `throw 'some string'` or `throw null`; scripts with syntax errors in some browsers; minified code throwing non-Error values; eval failures reported tersely by older Chromium builds.","solutions":["Make the in-page code throw Error objects (new Error(msg)) so descriptions are populated","Run the same expression manually in DevTools console to reproduce and see the real failure","Wrap the expression in try/catch and return the caught message as a value","Check for syntax errors — reformat the expression and validate it outside the driver"],"exampleFix":"// before\nObject r = driver.script(\"(function(){ throw 'boom'; })()\"); // 'Uncaught'\n// after\nObject r = driver.script(\"try { (function(){ throw new Error('boom'); })() } catch(e){ return e.message }\")","handlingStrategy":"try-catch","validationCode":"// wrap user expressions so errors surface as values, not exceptions\nString wrapped = \"try { JSON.stringify({ ok:true, value: eval(expr) }) } catch(e){ JSON.stringify({ ok:false, message: String(e) }) }\";","typeGuard":null,"tryCatchPattern":"try { Object v = driver.script(expr); } catch (RuntimeException e) { if (e.getMessage().equals(\"JS exception: Uncaught\")) { // description absent; rerun with try/catch wrapper to get the real message\n  return diagnose(expr); } throw e; }","preventionTips":["Throw Error objects (new Error(...)) in in-page code so descriptions are present","Avoid throwing bare strings/values from page code","Wrap third-party expressions in try/catch returning structured results","Reproduce failures in the DevTools console when only 'Uncaught' is reported"],"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"}