Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots πŸ“–

fix(desktop): reject a request whose reply is the error its handler threw

Luis Ortiz904a0e80157d
diff --git a/apps/desktop/src/main/__tests__/ask.test.ts b/apps/desktop/src/main/__tests__/ask.test.ts
new file mode 100644
index 0000000..ef0de77
--- /dev/null
+++ b/apps/desktop/src/main/__tests__/ask.test.ts
@@ -0,0 +1,32 @@
+import type { WebContents } from 'electron';
+import { describe, expect, it, vi } from 'vitest';
+
+const listeners = new Map<string, (event: unknown, result: unknown) => void>();
+
+vi.mock('electron', () => ({
+ ipcMain: {
+ once: (channel: string, listener: (event: unknown, result: unknown) => void) => listeners.set(channel, listener),
+ removeAllListeners: (channel: string) => listeners.delete(channel),
+ },
+}));
+
+import { ask } from '../ask';
+
+function renderer(reply: unknown): WebContents {
+ return {
+ isDestroyed: () => false,
+ send: (_channel: string, replyChannel: string) => listeners.get(replyChannel)?.({}, reply),
+ } as unknown as WebContents;
+}
+
+describe('ask', () => {
+ it('resolves with what the renderer answered', async () => {
+ await expect(ask(renderer('guide-1'), 'mimik:capture:startGuide')).resolves.toBe('guide-1');
+ });
+
+ it('rejects when the renderer answered with the error its handler threw', async () => {
+ await expect(ask(renderer({ error: 'QuotaExceededError' }), 'mimik:capture:startGuide')).rejects.toThrow(
+ 'mimik:capture:startGuide: QuotaExceededError',
+ );
+ });
+});
diff --git a/apps/desktop/src/main/ask.ts b/apps/desktop/src/main/ask.ts
index dfcb4de..5139452 100644
--- a/apps/desktop/src/main/ask.ts
+++ b/apps/desktop/src/main/ask.ts
@@ -12,9 +12,11 @@ export function ask<T>(target: WebContents | null, channel: string, payload?: un
reject(new Error(`${channel}: renderer did not reply within ${timeoutMs}ms`));
}, timeoutMs);
- ipcMain.once(replyChannel, (_event, result: T) => {
+ ipcMain.once(replyChannel, (_event, result: unknown) => {
clearTimeout(timer);
- resolve(result);
+ const failure = result && typeof result === 'object' && 'error' in result ? result.error : undefined;
+ if (typeof failure === 'string') reject(new Error(`${channel}: ${failure}`));
+ else resolve(result as T);
});
target.send(channel, replyChannel, payload);