Skip to content

Commit 5eeecd8

Browse files
committed
fix: wrap errors correctly with custom IPC methods
Signed-off-by: Florent Benoit <[email protected]>
1 parent 47bdca7 commit 5eeecd8

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

packages/main/src/plugin/docker-extension/docker-desktop-installation.ts

+27-3
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,30 @@ export class DockerDesktopInstallation {
161161
});
162162
});
163163
}
164+
// encode the error to be sent over IPC
165+
// this is needed because on the client it will display
166+
// a generic error message 'Error invoking remote method' and
167+
// it's not useful for end user
168+
encodeIpcError(e: unknown): { name?: string; message: unknown; extra?: Record<string, unknown> } {
169+
let builtError;
170+
if (e instanceof Error) {
171+
builtError = { name: e.name, message: e.message, extra: { ...e } };
172+
} else {
173+
builtError = { message: e };
174+
}
175+
return builtError;
176+
}
177+
178+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
179+
ipcHandle(channel: string, listener: (event: IpcMainInvokeEvent, ...args: any[]) => Promise<void> | any): any {
180+
ipcMain.handle(channel, async (...args) => {
181+
try {
182+
return { result: await Promise.resolve(listener(...args)) };
183+
} catch (e) {
184+
return { error: this.encodeIpcError(e) };
185+
}
186+
});
187+
}
164188

165189
async init(): Promise<void> {
166190
ipcMain.handle('docker-desktop-plugin:get-preload-script', async (): Promise<string> => {
@@ -177,10 +201,10 @@ export class DockerDesktopInstallation {
177201
},
178202
);
179203

180-
ipcMain.handle(
204+
this.ipcHandle(
181205
'docker-desktop-plugin:delete',
182-
async (event: IpcMainInvokeEvent, extensionName: string): Promise<void> => {
183-
return this.contributionManager.deleteExtension(extensionName);
206+
async (event: IpcMainInvokeEvent, extensionId: string): Promise<void> => {
207+
return this.contributionManager.deleteExtension(extensionId);
184208
},
185209
);
186210

packages/preload/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1584,8 +1584,8 @@ export function initExposure(): void {
15841584
}
15851585
});
15861586

1587-
contextBridge.exposeInMainWorld('ddExtensionDelete', async (extensionName: string): Promise<void> => {
1588-
return ipcInvoke('docker-desktop-plugin:delete', extensionName);
1587+
contextBridge.exposeInMainWorld('ddExtensionDelete', async (extensionId: string): Promise<void> => {
1588+
return ipcInvoke('docker-desktop-plugin:delete', extensionId);
15891589
});
15901590

15911591
contextBridge.exposeInMainWorld('getWebviewPreloadPath', async (): Promise<string> => {

0 commit comments

Comments
 (0)