Skip to content

[Bug]: page.evaluate returns undefined when chromium errors out with 'Object reference chain is too long' #33997

@ShaunThayil

Description

@ShaunThayil

Version

1.49.1

Steps to reproduce

Snippet:

import { chromium } from 'playwright';

const browser = await chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();

await page.evaluate(`
    // creates a deeply nested object: { 1: { 2: { 3 : { 4: {} }}}}
    window.createDeepNesting = function(depth) {
      const obj = {};
      let temp = obj;
      for (let i = 0; i < depth; i++) {
        temp[i] = {};
        temp = temp[i];
      }
      return obj
    }

    window.createDeepNestedError = async function(depth) {
      let error = new Error("Testing Error");
      error.deepObj = createDeepNesting(depth); 
      throw error;
    }
` );

const result = await page.evaluate('createDeepNesting(1000)');
console.log("Result ", result); //undefined

const errorResult = await page.evaluate('createDeepNestedError(1000)');
console.log("Error Result: ", errorResult) // undefined
await browser.close();

Expected behavior

I expected the page.evaluate call to error out if the evaled function had errored out(which does happen for simple errors), after some digging found that chromium had errorred out with the error 'Object reference chain is too long' and Playwright was ignoring the chromium error when it had that message.

function rewriteError(error: Error): Protocol.Runtime.evaluateReturnValue {
if (error.message.includes('Object reference chain is too long'))
return { result: { type: 'undefined' } };
if (error.message.includes('Object couldn\'t be returned by value'))
return { result: { type: 'undefined' } };
if (error instanceof TypeError && error.message.startsWith('Converting circular structure to JSON'))
rewriteErrorMessage(error, error.message + ' Are you passing a nested JSHandle?');
if (!js.isJavaScriptErrorInEvaluate(error) && !isSessionClosedError(error))
throw new Error('Execution context was destroyed, most likely because of a navigation.');
throw error;
}

This is troublesome as it can hide eval errors. If the chromium error is thrown (along with some hint like in the case of circular reference error) , I can try to do something about it (which I resolved by removing the deeply nested object).

Actual behavior

Got undefined both times.

Additional context

No response

Environment

System:
    OS: macOS 14.6.1
    CPU: (10) arm64 Apple M2 Pro
    Memory: 134.53 MB / 16.00 GB
  Binaries:
    Node: 18.19.1 - ~/.nvm/versions/node/v18.19.1/bin/node
    npm: 10.8.1 - ~/.nvm/versions/node/v18.19.1/bin/npm
  Languages:
    Bash: 5.2.26 - /opt/homebrew/bin/bash
  npmPackages:
    playwright: ^1.45.0 => 1.49.1

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions