karatelabs/karate · error · RuntimeException

JS exception

Error message

JS exception: {text}

What it means

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.

Solutions

  1. Make the in-page code throw Error objects (new Error(msg)) so descriptions are populated
  2. Run the same expression manually in DevTools console to reproduce and see the real failure
  3. Wrap the expression in try/catch and return the caught message as a value
  4. Check for syntax errors — reformat the expression and validate it outside the driver

Example fix

// before
Object r = driver.script("(function(){ throw 'boom'; })()"); // 'Uncaught'
// after
Object r = driver.script("try { (function(){ throw new Error('boom'); })() } catch(e){ return e.message }")
Defensive patterns

Strategy: try-catch

Validate before calling

// wrap user expressions so errors surface as values, not exceptions
String wrapped = "try { JSON.stringify({ ok:true, value: eval(expr) }) } catch(e){ JSON.stringify({ ok:false, message: String(e) }) }";

Try / catch

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
  return diagnose(expr); } throw e; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/a97ef4507c813a44. Report an issue: GitHub.

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/driver/cdp/CdpDriver.java:2026

            if (message != null && message.contains("Object reference chain is too long")) {
                logger.debug("script result not serializable, returning null: {}", truncate(expression, 100));
                return null;
            }
            logger.warn("JS error for expression: {}", truncate(expression, 100));
            throw new RuntimeException("JS error: " + response.getError());
        }
        Object exceptionDetails = response.getResult("exceptionDetails");
        if (exceptionDetails != null) {
            // Try to get detailed error message from exception object
            String description = response.getResultAsString("exceptionDetails.exception.description");
            if (description != null && !description.isEmpty()) {
                logger.warn("JS exception for: {} -> {}", truncate(expression, 100), truncate(description, 200));
                throw new RuntimeException("JS exception: " + description);
            }
            // Fall back to text (usually just "Uncaught")
            String text = response.getResultAsString("exceptionDetails.text");
            logger.warn("JS exception for: {} -> {}", truncate(expression, 100), text);
            throw new RuntimeException("JS exception: " + text);
        }
        Object value = response.getResult("result.value");
        logger.trace("script result: {}", value);
        return value;
    }

    // ========== Screenshot ==========

    /**
     * Take screenshot and return PNG bytes.
     */
    public byte[] screenshot() {
        return screenshot(false);
    }

    /**
     * Take screenshot, optionally embed in report.
     */

View on GitHub (pinned to a22eb90246)