refactor: use error-utils for remote exception serialization / deserialization (#14788)

* refactor: use error-utils for remote exception serialization / deserialization

* fix internal process.type in sandboxed renderer
This commit is contained in:
Milan Burda 2018-09-26 07:44:55 +02:00 committed by Samuel Attard
parent 3df739fa89
commit b499d57cfd
4 changed files with 19 additions and 20 deletions

View file

@ -12,6 +12,7 @@ const { ipcMain, isPromise } = electron
const objectsRegistry = require('@electron/internal/browser/objects-registry') const objectsRegistry = require('@electron/internal/browser/objects-registry')
const bufferUtils = require('@electron/internal/common/buffer-utils') const bufferUtils = require('@electron/internal/common/buffer-utils')
const errorUtils = require('@electron/internal/common/error-utils')
const hasProp = {}.hasOwnProperty const hasProp = {}.hasOwnProperty
@ -138,9 +139,7 @@ const plainObjectToMeta = function (obj) {
const exceptionToMeta = function (sender, contextId, error) { const exceptionToMeta = function (sender, contextId, error) {
return { return {
type: 'exception', type: 'exception',
message: error.message, value: errorUtils.serialize(error)
stack: error.stack || error,
cause: valueToMeta(sender, contextId, error.cause)
} }
} }

View file

@ -11,10 +11,12 @@ const constructors = new Map([
]) ])
exports.deserialize = function (error) { exports.deserialize = function (error) {
if (error.__ELECTRON_SERIALIZED_ERROR__ && constructors.has(error.name)) { if (error && error.__ELECTRON_SERIALIZED_ERROR__ && constructors.has(error.name)) {
const constructor = constructors.get(error.name) const constructor = constructors.get(error.name)
const deserializedError = new constructor(error.message) const deserializedError = new constructor(error.message)
deserializedError.stack = error.stack deserializedError.stack = error.stack
deserializedError.from = error.from
deserializedError.cause = exports.deserialize(error.cause)
return deserializedError return deserializedError
} }
return error return error
@ -28,6 +30,8 @@ exports.serialize = function (error) {
message: error.message, message: error.message,
stack: error.stack, stack: error.stack,
name: error.name, name: error.name,
from: process.type,
cause: exports.serialize(error.cause),
__ELECTRON_SERIALIZED_ERROR__: true __ELECTRON_SERIALIZED_ERROR__: true
} }
} }

View file

@ -6,6 +6,7 @@ const resolvePromise = Promise.resolve.bind(Promise)
const CallbacksRegistry = require('@electron/internal/renderer/callbacks-registry') const CallbacksRegistry = require('@electron/internal/renderer/callbacks-registry')
const bufferUtils = require('@electron/internal/common/buffer-utils') const bufferUtils = require('@electron/internal/common/buffer-utils')
const errorUtils = require('@electron/internal/common/error-utils')
const callbacksRegistry = new CallbacksRegistry() const callbacksRegistry = new CallbacksRegistry()
const remoteObjectCache = v8Util.createIDWeakMap() const remoteObjectCache = v8Util.createIDWeakMap()
@ -217,7 +218,7 @@ function metaToValue (meta) {
promise: () => resolvePromise({ then: metaToValue(meta.then) }), promise: () => resolvePromise({ then: metaToValue(meta.then) }),
error: () => metaToPlainObject(meta), error: () => metaToPlainObject(meta),
date: () => new Date(meta.value), date: () => new Date(meta.value),
exception: () => { throw metaToException(meta) } exception: () => { throw errorUtils.deserialize(meta.value) }
} }
if (meta.type in types) { if (meta.type in types) {
@ -267,15 +268,6 @@ function metaToPlainObject (meta) {
return obj return obj
} }
// Construct an exception error from the meta.
function metaToException (meta) {
const error = new Error(`${meta.message}\n${meta.stack}`)
const remoteProcess = exports.process
error.from = remoteProcess ? remoteProcess.type : null
error.cause = metaToValue(meta.cause)
return error
}
function handleMessage (channel, handler) { function handleMessage (channel, handler) {
ipcRenderer.on(channel, (event, passedContextId, ...args) => { ipcRenderer.on(channel, (event, passedContextId, ...args) => {
if (passedContextId === contextId) { if (passedContextId === contextId) {

View file

@ -54,12 +54,16 @@ preloadProcess.getHeapStatistics = () => binding.getHeapStatistics()
preloadProcess.getSystemMemoryInfo = () => binding.getSystemMemoryInfo() preloadProcess.getSystemMemoryInfo = () => binding.getSystemMemoryInfo()
preloadProcess.getCPUUsage = () => binding.getCPUUsage() preloadProcess.getCPUUsage = () => binding.getCPUUsage()
preloadProcess.getIOCounters = () => binding.getIOCounters() preloadProcess.getIOCounters = () => binding.getIOCounters()
preloadProcess.argv = process.argv = binding.getArgv()
preloadProcess.execPath = process.execPath = binding.getExecPath() Object.assign(processProps, {
preloadProcess.pid = process.pid = binding.getPid() argv: binding.getArgv(),
preloadProcess.resourcesPath = binding.getResourcesPath() execPath: binding.getExecPath(),
preloadProcess.sandboxed = true pid: binding.getPid(),
preloadProcess.type = 'renderer' resourcesPath: binding.getResourcesPath(),
sandboxed: true,
type: 'renderer'
})
Object.assign(preloadProcess, processProps) Object.assign(preloadProcess, processProps)
Object.assign(process, processProps) Object.assign(process, processProps)