Skip to content

contextBridge.exposeInMainWorld does not copy Error details #25596

@rfricz

Description

@rfricz

Preflight Checklist

  • I have read the Contributing Guidelines for this project.
  • I agree to follow the Code of Conduct that this project adheres to.
  • I have searched the issue tracker for an issue that matches the one I want to file, without success.

Issue Details

When calling functions exposed by contextBridge.exposeInMainWorld, Promises rejected with Error objects or directly thrown Errors (and even caught Errors returned in a wrapper) are not copied as the documentation states – all important information except the message is removed, e.g. Common System Error properties like err.code, err.path etc. are gone. Also, the “Uncaught Error: ” message prefix is not very helpful.

  • Electron Version:
    • 10.1.2
  • Operating System:
    • Windows 10 (2004)

Expected Behavior

Instances of the Error class (or inherited from it) coming from the context bridge should have all properties preserved (except err.stack, obviously) and message left intact to allow meaningful error handling. Structured Clone Algorithm otherwise serializes Error objects correctly.

Actual Behavior

The Error object is stripped of important additional properties and has a confusing message saying it’s uncaught even when caught on both sides of the bridge.

To Reproduce

preload.js

const { contextBridge } = require('electron');
const fs = require('fs');

contextBridge.exposeInMainWorld('myapi', {
  request: () => fs.lstatSync('foo')
});

renderer

try {
  window.myapi.request();
} catch (e) {
  console.dir(e);
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions