fix ElectronHostHook path, exception handling

This commit is contained in:
Gregor Biswanger
2019-01-11 02:42:32 +01:00
parent 2b3eabf75a
commit 04968d088b
4 changed files with 28 additions and 5 deletions

View File

@@ -34,7 +34,15 @@ namespace ElectronNET.API
public void Call(string socketEventName, params dynamic[] arguments)
{
BridgeConnector.Socket.Emit(socketEventName, arguments);
string guid = Guid.NewGuid().ToString();
BridgeConnector.Socket.On(socketEventName + "Error" + guid, (result) =>
{
BridgeConnector.Socket.Off(socketEventName + "Error" + guid);
Electron.Dialog.ShowErrorBox("Host Hook Exception", result.ToString());
});
BridgeConnector.Socket.Emit(socketEventName, arguments, guid);
}
public Task<T> CallAsync<T>(string socketEventName, params dynamic[] arguments)
@@ -42,6 +50,12 @@ namespace ElectronNET.API
var taskCompletionSource = new TaskCompletionSource<T>();
string guid = Guid.NewGuid().ToString();
BridgeConnector.Socket.On(socketEventName + "Error" + guid, (result) =>
{
BridgeConnector.Socket.Off(socketEventName + "Error" + guid);
Electron.Dialog.ShowErrorBox("Host Hook Exception", result.ToString());
});
BridgeConnector.Socket.On(socketEventName + "Complete" + guid, (result) =>
{
BridgeConnector.Socket.Off(socketEventName + "Complete" + guid);

View File

@@ -5,9 +5,17 @@ export class Connector {
this.socket.on(key, (...args: any[]) => {
const id: string = args.pop();
javaScriptCode(args, (data) => {
this.socket.emit(`${key}Complete${id}`, data);
});
try {
javaScriptCode(...args, (data) => {
if (isNaN(data)) {
throw new Error('Result is NaN');
} else {
this.socket.emit(`${key}Complete${id}`, data);
}
});
} catch (error) {
this.socket.emit(`${key}Error${id}`, 'Host Hook Exception', error);
}
});
}
}

View File

@@ -1,4 +1,5 @@
import * as Electron from "electron";
import { Connector } from "./connector";
export class HookService extends Connector {
constructor(socket: SocketIO.Socket, public app: Electron.App) {

View File

@@ -106,7 +106,7 @@ function startSocketApiBridge(port) {
}
try {
const hostHookScriptFilePath = path.join(__dirname, 'bin', 'ElectronHostHook', 'index.js');
const hostHookScriptFilePath = path.join(__dirname, 'ElectronHostHook', 'index.js');
const { HookService } = require(hostHookScriptFilePath);
if (hostHook === undefined) {
hostHook = new HookService(socket, app);