8000 Throw custom error when remote object is missing from registry by kevinsawicki · Pull Request #8003 · electron/electron · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Throw custom error when remote object is missing from registry #8003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/browser/objects-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class ObjectsRegistry {

// Get an object according to its ID.
get (id) {
return this.storage[id].object
const pointer = this.storage[id]
if (pointer != null) return pointer.object
}

// Dereference an object according to its ID.
Expand Down
33 changes: 32 additions & 1 deletion lib/browser/rpc-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ const exceptionToMeta = function (error) {
}
}

const throwRPCError = function (message) {
const error = new Error(message)
error.code = 'EBADRPC'
error.errno = -72
throw error
}

// Convert array of meta data from renderer into array of real values.
const unwrapArgs = function (sender, args) {
const metaToValue = function (meta) {
Expand Down Expand Up @@ -277,6 +284,10 @@ ipcMain.on('ELECTRON_BROWSER_CONSTRUCTOR', function (event, id, args) {
args = unwrapArgs(event.sender, args)
let constructor = objectsRegistry.get(id)

if (constructor == null) {
throwRPCError(`Cannot call constructor on missing remote object ${id}`)
}

// Call new with array of arguments.
// http://stackoverflow.com/questions/1606797/use-of-apply-with-new-operator-is-this-possible
let obj = new (Function.prototype.bind.apply(constructor, [null].concat(args)))()
Expand All @@ -290,6 +301,11 @@ ipcMain.on('ELECTRON_BROWSER_FUNCTION_CALL', function (event, id, args) {
try {
args = unwrapArgs(event.sender, args)
let func = objectsRegistry.get(id)

if (func == null) {
throwRPCError(`Cannot call function on missing remote object ${id}`)
}

callFunction(event, func, global, args)
} catch (error) {
event.returnValue = exceptionToMeta(error)
Expand All @@ -299,9 +315,14 @@ ipcMain.on('ELECTRON_BROWSER_FUNCTION_CALL', function (event, id, args) {
ipcMain.on('ELECTRON_BROWSER_MEMBER_CONSTRUCTOR', function (event, id, method, args) {
try {
args = unwrapArgs(event.sender, args)
let constructor = objectsRegistry.get(id)[method]
let object = objectsRegistry.get(id)

if (object == null) {
throwRPCError(`Cannot call constructor '${method}' on missing remote object ${id}`)
}

// Call new with array of arguments.
let constructor = object[method]
let obj = new (Function.prototype.bind.apply(constructor, [null].concat(args)))()
event.returnValue = valueToMeta(event.sender, obj)
} catch (error) {
Expand All @@ -313,6 +334,11 @@ ipcMain.on('ELECTRON_BROWSER_MEMBER_CALL', function (event, id, method, args) {
try {
args = unwrapArgs(event.sender, args)
let obj = objectsRegistry.get(id)

if (obj == null) {
throwRPCError(`Cannot call function '${method}' on missing remote object ${id}`)
}

callFunction(event, obj[method], obj, args)
} catch (error) {
event.returnValue = exceptionToMeta(error)
Expand All @@ -322,6 +348,11 @@ ipcMain.on('ELECTRON_BROWSER_MEMBER_CALL', function (event, id, method, args) {
ipcMain.on('ELECTRON_BROWSER_MEMBER_SET', function (event, id, name, value) {
try {
let obj = objectsRegistry.get(id)

if (obj == null) {
throwRPCError(`Cannot set property '${name}' on missing remote object ${id}`)
}

obj[name] = value
event.returnValue = null
} catch (error) {
Expand Down
0