diff --git a/lib/browser/remote/server.ts b/lib/browser/remote/server.ts index 970dbf983da9..d1cfab1e8019 100644 --- a/lib/browser/remote/server.ts +++ b/lib/browser/remote/server.ts @@ -245,6 +245,17 @@ type MetaTypeFromRenderer = { length: number } +const fakeConstructor = (constructor: Function, name: string) => + new Proxy(Object, { + get (target, prop, receiver) { + if (prop === 'name') { + return name + } else { + return Reflect.get(target, prop, receiver) + } + } + }) + // Convert array of meta data from renderer into array of real values. const unwrapArgs = function (sender: electron.WebContents, frameId: number, contextId: string, args: any[]) { const metaToValue = function (meta: MetaTypeFromRenderer): any { @@ -262,8 +273,9 @@ const unwrapArgs = function (sender: electron.WebContents, frameId: number, cont then: metaToValue(meta.then) }) case 'object': { - const ret: any = {} - Object.defineProperty(ret.constructor, 'name', { value: meta.name }) + const ret: any = meta.name !== 'Object' ? Object.create({ + constructor: fakeConstructor(Object, meta.name) + }) : {} for (const { name, value } of meta.members) { ret[name] = metaToValue(value) diff --git a/lib/renderer/api/remote.js b/lib/renderer/api/remote.js index 07c224b70211..ebe6e62ecbda 100644 --- a/lib/renderer/api/remote.js +++ b/lib/renderer/api/remote.js @@ -64,6 +64,11 @@ function wrapArgs (args, visited = new Set()) { type: 'remote-object', id: v8Util.getHiddenValue(value, 'atomId') } + } else if (value instanceof Error) { + return { + type: 'value', + value + } } const meta = { diff --git a/spec/api-remote-spec.js b/spec/api-remote-spec.js index 2790fd4124ee..1231bdb35245 100644 --- a/spec/api-remote-spec.js +++ b/spec/api-remote-spec.js @@ -498,17 +498,16 @@ ifdescribe(features.isRemoteModuleEnabled())('remote module', () => { it('throws errors from the main process', () => { expect(() => { throwFunction() - }).to.throw() + }).to.throw(/undefined/) }) - it('throws custom errors from the main process', () => { - const err = new Error('error') - err.cause = new Error('cause') - err.prop = 'error prop' + it('tracks error cause', () => { try { - throwFunction(err) - } catch (error) { - expect(error.cause).to.deep.equal(...resolveGetters(err)) + throwFunction(new Error('error from main')) + expect.fail() + } catch (e) { + expect(e.message).to.match(/Could not call remote function/) + expect(e.cause.message).to.equal('error from main') } }) })