Skip to content

stucturedClone defective for DataCloneErrorΒ #49181

Open
@corrideat

Description

@corrideat

Version

v19.9.0

Platform

Linux WORKSTATION 5.15.90.1-microsoft-standard-WSL2 #1 SMP Fri Jan 27 02:56:13 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

Run the following snippet:

const [e, c] = (() => {
  try {
    // Fails because functions can't be cloned
    structuredClone(()=>{});
  } catch (e) {
    // Return the original error and a clone
    return [e, structuredClone(e)];
  }
})();
console.log({
    'e instanceof Error': e instanceof Error,
    'e.name': e.name,
    'e.message': e.message,
    'e.stack': e.stack,
}, {
    'c instanceof Error': c instanceof Error,
    'c.name': c.name,
    'c.message': c.message,
    'c.stack': c.stack,
});

How often does it reproduce? Is there a required condition?

Always

What is the expected behavior? Why is that the expected behavior?

Since structuredClone is supposed to support errors, the result should not be an empty object. It should be either a copy of the original error, or a copy of the origininal error but with name changed to Error.

What do you see instead?

The cloned DataCloneError cloned as an empty object.

Welcome to Node.js v19.9.0.
Type ".help" for more information.
> const [e, c] = (() => {
...   try {
...     // Fails because functions can't be cloned
...     structuredClone(()=>{});
...   } catch (e) {
...     // Return the original error and a clone
...     return [e, structuredClone(e)];
...   }
... })();
undefined
> console.log({
...     'e instanceof Error': e instanceof Error,
...     'e.name': e.name,
...     'e.message': e.message,
...     'e.stack': e.stack,
... }, {
...     'c instanceof Error': c instanceof Error,
...     'c.name': c.name,
...     'c.message': c.message,
...     'c.stack': c.stack,
... });
{
  'e instanceof Error': true,
  'e.name': 'DataCloneError',
  'e.message': '()=>{} could not be cloned.',
  'e.stack': 'DataCloneError: ()=>{} could not be cloned.\n' +
    '    at new DOMException (node:internal/per_context/domexception:53:5)\n' +
    '    at structuredClone (node:internal/structured_clone:23:17)\n' +
    '    at REPL9:4:5\n' +
    '    at REPL9:9:3\n' +
    '    at Script.runInThisContext (node:vm:128:12)\n' +
    '    at REPLServer.defaultEval (node:repl:570:29)\n' +
    '    at bound (node:domain:433:15)\n' +
    '    at REPLServer.runBound [as eval] (node:domain:444:12)\n' +
    '    at REPLServer.onLine (node:repl:900:10)\n' +
    '    at REPLServer.emit (node:events:525:35)'
} {
  'c instanceof Error': false,
  'c.name': undefined,
  'c.message': undefined,
  'c.stack': undefined
}

Additional information

This error limits sending back useful error information through MessagePorts

This is the result of running the same snippet in other runtimes.

Deno

Deno 1.35.2
exit using ctrl+d, ctrl+c, or close()
REPL is running with all permissions allowed.
To specify permissions, run `deno repl` with allow flags.
> const [e, c] = (() => {
  try {
    // Fails because functions can't be cloned
    structuredClone(()=>{});
  } catch (e) {
    // Return the original error and a clone
    return [e, structuredClone(e)];
  }
})();
console.log({
    'e instanceof Error': e instanceof Error,
    'e.name': e.name,
    'e.message': e.message,
    'e.stack': e.stack,
}, {
    'c instanceof Error': c instanceof Error,
    'c.name': c.name,
    'c.message': c.message,
    'c.stack': c.stack,
});

{
  "e instanceof Error": true,
  "e.name": "DataCloneError",
  "e.message": "()=>{} could not be cloned.",
  "e.stack": "DOMException: ()=>{} could not be cloned.\n" +
    "    at new DOMException (ext:deno_web/01_dom_exception.js:"... 288 more characters
} {
  "c instanceof Error": false,
  "c.name": undefined,
  "c.message": undefined,
  "c.stack": undefined
}
undefined

Microsoft Edge

const [e, c] = (() => {
  try {
    // Fails because functions can't be cloned
    structuredClone(()=>{});
  } catch (e) {
    // Return the original error and a clone
    return [e, structuredClone(e)];
  }
})();
console.log({
    'e instanceof Error': e instanceof Error,
    'e.name': e.name,
    'e.message': e.message,
    'e.stack': e.stack,
}, {
    'c instanceof Error': c instanceof Error,
    'c.name': c.name,
    'c.message': c.message,
    'c.stack': c.stack,
});

{
    "e instanceof Error": true,
    "e.name": "DataCloneError",
    "e.message": "Failed to execute 'structuredClone' on 'Window': ()=>{} could not be cloned.",
    "e.stack": "Error: Failed to execute 'structuredClone' on 'Window': ()=>{} could not be cloned.\n    at <anonymous>:4:5\n    at <anonymous>:9:3"
}
{
    "c instanceof Error": true,
    "c.name": "DataCloneError",
    "c.message": "Failed to execute 'structuredClone' on 'Window': ()=>{} could not be cloned."
}

Mozilla Firefox

const [e, c] = (() => {
  try {
    // Fails because functions can't be cloned
    structuredClone(()=>{});
  } catch (e) {
    // Return the original error and a clone
    return [e, structuredClone(e)];
  }
})();
console.log({
    'e instanceof Error': e instanceof Error,
    'e.name': e.name,
    'e.message': e.message,
    'e.stack': e.stack,
}, {
    'c instanceof Error': c instanceof Error,
    'c.name': c.name,
    'c.message': c.message,
    'c.stack': c.stack,
});

{
  "e instanceof Error": true,
  "e.name": "DataCloneError",
  "e.message": "The object could not be cloned.",
  "e.stack": "@debugger eval code:4:20\n@debugger eval code:9:3\n"
}
{
  "c instanceof Error": true,
  "c.name": "DataCloneError",
  "c.message": "The object could not be cloned.",
  "c.stack": ""
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    web-standardsIssues and PRs related to Web APIs

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions