Skip to content

Commit 04968d0

Browse files
fix ElectronHostHook path, exception handling
1 parent 2b3eabf commit 04968d0

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

ElectronNET.API/HostHook.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,28 @@ internal static HostHook Instance
3434

3535
public void Call(string socketEventName, params dynamic[] arguments)
3636
{
37-
BridgeConnector.Socket.Emit(socketEventName, arguments);
37+
string guid = Guid.NewGuid().ToString();
38+
39+
BridgeConnector.Socket.On(socketEventName + "Error" + guid, (result) =>
40+
{
41+
BridgeConnector.Socket.Off(socketEventName + "Error" + guid);
42+
Electron.Dialog.ShowErrorBox("Host Hook Exception", result.ToString());
43+
});
44+
45+
BridgeConnector.Socket.Emit(socketEventName, arguments, guid);
3846
}
3947

4048
public Task<T> CallAsync<T>(string socketEventName, params dynamic[] arguments)
4149
{
4250
var taskCompletionSource = new TaskCompletionSource<T>();
4351
string guid = Guid.NewGuid().ToString();
4452

53+
BridgeConnector.Socket.On(socketEventName + "Error" + guid, (result) =>
54+
{
55+
BridgeConnector.Socket.Off(socketEventName + "Error" + guid);
56+
Electron.Dialog.ShowErrorBox("Host Hook Exception", result.ToString());
57+
});
58+
4559
BridgeConnector.Socket.On(socketEventName + "Complete" + guid, (result) =>
4660
{
4761
BridgeConnector.Socket.Off(socketEventName + "Complete" + guid);

ElectronNET.Host/ElectronHostHook/connector.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,17 @@ export class Connector {
55
this.socket.on(key, (...args: any[]) => {
66
const id: string = args.pop();
77

8-
javaScriptCode(args, (data) => {
9-
this.socket.emit(`${key}Complete${id}`, data);
10-
});
8+
try {
9+
javaScriptCode(...args, (data) => {
10+
if (isNaN(data)) {
11+
throw new Error('Result is NaN');
12+
} else {
13+
this.socket.emit(`${key}Complete${id}`, data);
14+
}
15+
});
16+
} catch (error) {
17+
this.socket.emit(`${key}Error${id}`, 'Host Hook Exception', error);
18+
}
1119
});
1220
}
1321
}

ElectronNET.Host/ElectronHostHook/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as Electron from "electron";
2+
import { Connector } from "./connector";
23

34
export class HookService extends Connector {
45
constructor(socket: SocketIO.Socket, public app: Electron.App) {

ElectronNET.Host/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ function startSocketApiBridge(port) {
106106
}
107107

108108
try {
109-
const hostHookScriptFilePath = path.join(__dirname, 'bin', 'ElectronHostHook', 'index.js');
109+
const hostHookScriptFilePath = path.join(__dirname, 'ElectronHostHook', 'index.js');
110110
const { HookService } = require(hostHookScriptFilePath);
111111
if (hostHook === undefined) {
112112
hostHook = new HookService(socket, app);

0 commit comments

Comments
 (0)