// Convert a real value into meta data.
let valueToMeta = function (sender, value, optimizeSimpleObject = false) {
// Determine the type of value.
- let meta = { type: typeof value }
+ const meta = { type: typeof value }
if (meta.type === 'object') {
// Recognize certain types of objects.
if (value === null) {
} else if (meta.type === 'buffer') {
meta.value = Array.prototype.slice.call(value, 0)
} else if (meta.type === 'promise') {
+ // Add default handler to prevent unhandled rejections in main process
+ // Instead they should appear in the renderer process
+ value.then(function () {}, function () {})
+
meta.then = valueToMeta(sender, function (onFulfilled, onRejected) {
value.then(onFulfilled, onRejected)
})
}
// Convert object to meta by value.
-var plainObjectToMeta = function (obj) {
+const plainObjectToMeta = function (obj) {
return Object.getOwnPropertyNames(obj).map(function (name) {
return {
name: name,
}
// Convert Error into meta data.
-var exceptionToMeta = function (error) {
+const exceptionToMeta = function (error) {
return {
type: 'exception',
message: error.message,
}
// Convert array of meta data from renderer into array of real values.
-var unwrapArgs = function (sender, args) {
- var metaToValue
- metaToValue = function (meta) {
- var i, len, member, ref, returnValue
+const unwrapArgs = function (sender, args) {
+ const metaToValue = function (meta) {
+ let i, len, member, ref, returnValue
switch (meta.type) {
case 'value':
return meta.value
// Call a function and send reply asynchronously if it's a an asynchronous
// style function and the caller didn't pass a callback.
-var callFunction = function (event, func, caller, args) {
- var funcMarkedAsync, funcName, funcPassedCallback, ref, ret
+const callFunction = function (event, func, caller, args) {
+ let funcMarkedAsync, funcName, funcPassedCallback, ref, ret
funcMarkedAsync = v8Util.getHiddenValue(func, 'asynchronous')
funcPassedCallback = typeof args[args.length - 1] === 'function'
try {
args.push(function (ret) {
event.returnValue = valueToMeta(event.sender, ret, true)
})
- return func.apply(caller, args)
+ func.apply(caller, args)
} else {
ret = func.apply(caller, args)
event.returnValue = valueToMeta(event.sender, ret, true)
done()
})
})
+
+ it('does not emit unhandled rejection events in the main process', function (done) {
+ remote.process.once('unhandledRejection', function (reason) {
+ done(reason)
+ })
+
+ var promise = remote.require(path.join(fixtures, 'module', 'unhandled-rejection.js'))
+ promise.reject().then(function () {
+ done(new Error('Promise was not rejected'))
+ }).catch(function (error) {
+ assert.equal(error.message, 'rejected')
+ done()
+ })
+ })
+
+ it('emits unhandled rejection events in the renderer process', function (done) {
+ window.addEventListener('unhandledrejection', function (event) {
+ event.preventDefault()
+ assert.equal(event.reason.message, 'rejected')
+ done()
+ })
+
+ var promise = remote.require(path.join(fixtures, 'module', 'unhandled-rejection.js'))
+ promise.reject().then(function () {
+ done(new Error('Promise was not rejected'))
+ })
+ })
})
describe('remote webContents', function () {