-
Notifications
You must be signed in to change notification settings - Fork 2k
Serialize IPC errors #61665
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
+189
−78
Merged
Serialize IPC errors #61665
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1d2ce7f
Serialize all enumerable error fields
gzdunek 58598c1
Add wrappers around `ipcMain.handle` and `ipcRenderer.invoke`
gzdunek d1d5a79
Fix `Method Error.prototype.toString called on incompatible receiver …
gzdunek e673d02
Improve docs
gzdunek 4d1a287
Lint
gzdunek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
web/packages/teleterm/src/mainProcess/ipcSerializer.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /** | ||
| * Teleport | ||
| * Copyright (C) 2025 Gravitational, Inc. | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| import { TshdRpcError } from 'teleterm/services/tshd/cloneableClient'; | ||
|
|
||
| import { deserializeError, serializeError } from './ipcSerializer'; | ||
|
|
||
| test('serializes and deserializes regular error', () => { | ||
| const err = new Error('Boom'); | ||
| err.name = 'CustomError'; | ||
|
|
||
| const serialized = serializeError(err); | ||
| expect(serialized instanceof Error).toBe(false); | ||
| expect(serialized.message).toBe('Boom'); | ||
| expect(serialized.name).toBe('CustomError'); | ||
| expect(serialized.stack).toBe(err.stack); | ||
| expect(serialized.cause).toBe(err.cause); | ||
|
|
||
| const cloned = structuredClone(serialized); | ||
| const deserialized = deserializeError(cloned); | ||
| expect(deserialized instanceof Error).toBe(true); | ||
| expect(deserialized.message).toBe('Boom'); | ||
| expect(deserialized.name).toBe('CustomError'); | ||
| expect(deserialized.stack).toBe(err.stack); | ||
| expect(deserialized.cause).toBe(err.cause); | ||
| }); | ||
|
|
||
| test('serializes and deserializes tshd error', () => { | ||
| const err: TshdRpcError = { | ||
| name: 'TshdRpcError', | ||
| message: 'Could not found', | ||
| toString: () => 'Could not found', | ||
| cause: '', | ||
| stack: '', | ||
| code: 'NOT_FOUND', | ||
| isResolvableWithRelogin: false, | ||
| }; | ||
|
|
||
| const serialized = serializeError(err); | ||
| expect(serialized instanceof Error).toBe(false); | ||
| expect(serialized.message).toBe('Could not found'); | ||
| expect(serialized.name).toBe('TshdRpcError'); | ||
| expect(serialized['isResolvableWithRelogin']).toBe(false); | ||
| expect(serialized['code']).toBe('NOT_FOUND'); | ||
|
|
||
| const cloned = structuredClone(serialized); | ||
| const deserialized = deserializeError(cloned); | ||
| expect(deserialized instanceof Error).toBe(true); | ||
| expect(deserialized.message).toBe('Could not found'); | ||
| expect(deserialized.name).toBe('TshdRpcError'); | ||
| expect(deserialized['isResolvableWithRelogin']).toBe(false); | ||
| expect(deserialized['code']).toBe('NOT_FOUND'); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ import { | |
| app, | ||
| dialog, | ||
| ipcMain, | ||
| IpcMainInvokeEvent, | ||
| Menu, | ||
| MenuItemConstructorOptions, | ||
| nativeTheme, | ||
|
|
@@ -415,17 +416,17 @@ export default class MainProcess { | |
| event.returnValue = nativeTheme.shouldUseDarkColors; | ||
| }); | ||
|
|
||
| ipcMain.handle('main-process-get-resolved-child-process-addresses', () => { | ||
| ipcHandle('main-process-get-resolved-child-process-addresses', () => { | ||
| return this.resolvedChildProcessAddresses; | ||
| }); | ||
|
|
||
| ipcMain.handle('main-process-show-file-save-dialog', (_, filePath) => | ||
| ipcHandle('main-process-show-file-save-dialog', (_, filePath) => | ||
| dialog.showSaveDialog({ | ||
| defaultPath: path.basename(filePath), | ||
| }) | ||
| ); | ||
|
|
||
| ipcMain.handle( | ||
| ipcHandle( | ||
| MainProcessIpc.SaveTextToFile, | ||
| async ( | ||
| _, | ||
|
|
@@ -464,7 +465,7 @@ export default class MainProcess { | |
| } | ||
| ); | ||
|
|
||
| ipcMain.handle( | ||
| ipcHandle( | ||
| MainProcessIpc.ForceFocusWindow, | ||
| async ( | ||
| _, | ||
|
|
@@ -485,7 +486,7 @@ export default class MainProcess { | |
| // Used in the `tsh install` command on macOS to make the bundled tsh available in PATH. | ||
| // Returns true if tsh got successfully installed, false if the user closed the osascript | ||
| // prompt. Throws an error when osascript fails. | ||
| ipcMain.handle('main-process-symlink-tsh-macos', async () => { | ||
| ipcHandle('main-process-symlink-tsh-macos', async () => { | ||
| const source = this.settings.tshd.binaryPath; | ||
| const target = '/usr/local/bin/tsh'; | ||
| const prompt = | ||
|
|
@@ -507,7 +508,7 @@ export default class MainProcess { | |
| } | ||
| }); | ||
|
|
||
| ipcMain.handle('main-process-remove-tsh-symlink-macos', async () => { | ||
| ipcHandle('main-process-remove-tsh-symlink-macos', async () => { | ||
| const target = '/usr/local/bin/tsh'; | ||
| const prompt = | ||
| 'Teleport Connect wants to remove a symlink for tsh from /usr/local/bin.'; | ||
|
|
@@ -528,21 +529,21 @@ export default class MainProcess { | |
| } | ||
| }); | ||
|
|
||
| ipcMain.handle('main-process-open-config-file', async () => { | ||
| ipcHandle('main-process-open-config-file', async () => { | ||
| const path = this.configFileStorage.getFilePath(); | ||
| await shell.openPath(path); | ||
| return path; | ||
| }); | ||
|
|
||
| ipcMain.handle(MainProcessIpc.DownloadConnectMyComputerAgent, () => | ||
| ipcHandle(MainProcessIpc.DownloadConnectMyComputerAgent, () => | ||
| this.downloadAgentShared() | ||
| ); | ||
|
|
||
| ipcMain.handle(MainProcessIpc.VerifyConnectMyComputerAgent, async () => { | ||
| ipcHandle(MainProcessIpc.VerifyConnectMyComputerAgent, async () => { | ||
| await verifyAgent(this.settings.agentBinaryPath); | ||
| }); | ||
|
|
||
| ipcMain.handle( | ||
| ipcHandle( | ||
| 'main-process-connect-my-computer-create-agent-config-file', | ||
| (_, args: CreateAgentConfigFileArgs) => | ||
| createAgentConfigFile(this.settings, { | ||
|
|
@@ -553,7 +554,7 @@ export default class MainProcess { | |
| }) | ||
| ); | ||
|
|
||
| ipcMain.handle( | ||
| ipcHandle( | ||
| 'main-process-connect-my-computer-is-agent-config-file-created', | ||
| async ( | ||
| _, | ||
|
|
@@ -563,7 +564,7 @@ export default class MainProcess { | |
| ) => isAgentConfigFileCreated(this.settings, args.rootClusterUri) | ||
| ); | ||
|
|
||
| ipcMain.handle( | ||
| ipcHandle( | ||
| 'main-process-connect-my-computer-kill-agent', | ||
| async ( | ||
| _, | ||
|
|
@@ -575,7 +576,7 @@ export default class MainProcess { | |
| } | ||
| ); | ||
|
|
||
| ipcMain.handle( | ||
| ipcHandle( | ||
| 'main-process-connect-my-computer-remove-agent-directory', | ||
| ( | ||
| _, | ||
|
|
@@ -585,11 +586,11 @@ export default class MainProcess { | |
| ) => removeAgentDirectory(this.settings, args.rootClusterUri) | ||
| ); | ||
|
|
||
| ipcMain.handle(MainProcessIpc.TryRemoveConnectMyComputerAgentBinary, () => | ||
| ipcHandle(MainProcessIpc.TryRemoveConnectMyComputerAgentBinary, () => | ||
| this.agentRunner.tryRemoveAgentBinary() | ||
| ); | ||
|
|
||
| ipcMain.handle( | ||
| ipcHandle( | ||
| 'main-process-connect-my-computer-run-agent', | ||
| async ( | ||
| _, | ||
|
|
@@ -625,7 +626,7 @@ export default class MainProcess { | |
| } | ||
| ); | ||
|
|
||
| ipcMain.handle( | ||
| ipcHandle( | ||
| 'main-process-open-agent-logs-directory', | ||
| async ( | ||
| _, | ||
|
|
@@ -644,7 +645,7 @@ export default class MainProcess { | |
| } | ||
| ); | ||
|
|
||
| ipcMain.handle( | ||
| ipcHandle( | ||
| MainProcessIpc.SelectDirectoryForDesktopSession, | ||
| async (_, args: { desktopUri: string; login: string }) => { | ||
| const value = await dialog.showOpenDialog({ | ||
|
|
@@ -673,11 +674,11 @@ export default class MainProcess { | |
| event.returnValue = this.appUpdater.supportsUpdates(); | ||
| }); | ||
|
|
||
| ipcMain.handle(MainProcessIpc.CheckForAppUpdates, () => | ||
| ipcHandle(MainProcessIpc.CheckForAppUpdates, () => | ||
| this.appUpdater.checkForUpdates() | ||
| ); | ||
|
|
||
| ipcMain.handle( | ||
| ipcHandle( | ||
| MainProcessIpc.ChangeAppUpdatesManagingCluster, | ||
| ( | ||
| event, | ||
|
|
@@ -687,31 +688,31 @@ export default class MainProcess { | |
| ) => this.appUpdater.changeManagingCluster(args.clusterUri) | ||
| ); | ||
|
|
||
| ipcMain.handle(MainProcessIpc.DownloadAppUpdate, () => | ||
| ipcHandle(MainProcessIpc.DownloadAppUpdate, () => | ||
| this.appUpdater.download() | ||
| ); | ||
|
|
||
| ipcMain.handle(MainProcessIpc.CancelAppUpdateDownload, () => | ||
| ipcHandle(MainProcessIpc.CancelAppUpdateDownload, () => | ||
| this.appUpdater.cancelDownload() | ||
| ); | ||
|
|
||
| ipcMain.handle(MainProcessIpc.QuiteAndInstallAppUpdate, () => | ||
| ipcHandle(MainProcessIpc.QuiteAndInstallAppUpdate, () => | ||
| this.appUpdater.quitAndInstall() | ||
| ); | ||
|
|
||
| ipcMain.handle(MainProcessIpc.AddCluster, (ev, proxyAddress) => | ||
| ipcHandle(MainProcessIpc.AddCluster, (ev, proxyAddress) => | ||
| this.clusterLifecycleManager.addCluster(proxyAddress) | ||
| ); | ||
|
|
||
| ipcMain.handle(MainProcessIpc.SyncRootClusters, () => | ||
| ipcHandle(MainProcessIpc.SyncRootClusters, () => | ||
| this.clusterLifecycleManager.syncRootClustersAndStartProfileWatcher() | ||
| ); | ||
|
|
||
| ipcMain.handle(MainProcessIpc.SyncCluster, (_, args) => | ||
| ipcHandle(MainProcessIpc.SyncCluster, (_, args) => | ||
| this.clusterLifecycleManager.syncCluster(args.clusterUri) | ||
| ); | ||
|
|
||
| ipcMain.handle(MainProcessIpc.Logout, async (_, args) => { | ||
| ipcHandle(MainProcessIpc.Logout, async (_, args) => { | ||
| await this.clusterLifecycleManager.logoutAndRemoveCluster( | ||
| args.clusterUri | ||
| ); | ||
|
|
@@ -1010,3 +1011,23 @@ function makeAppUpdaterStorage(fs: FileStorage): AppUpdaterStorage { | |
| }, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Handles requests sent via `ipcInvoke`. | ||
| * The renderer must send requests using `ipcInvoke` (not `ipcRenderer.invoke`). | ||
| * | ||
| * Use this instead of `ipcMain.handle`. It ensures full error serialization | ||
| * and prevents Electron from adding the generic message "Error invoking remote method". | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the JSDoc for |
||
| */ | ||
| function ipcHandle( | ||
| channel: string, | ||
| listener: (event: IpcMainInvokeEvent, ...args: any[]) => Promise<any> | any | ||
| ): void { | ||
| ipcMain.handle(channel, async (...args) => { | ||
| try { | ||
| return { result: await Promise.try(listener, ...args) }; | ||
| } catch (e) { | ||
| return { error: serializeError(e) }; | ||
| } | ||
| }); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to test this by turning off my wifi while the agent was being downloaded and I got this:
Is this caused by this change? Would it be possible to add a test for that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for catching it, turns out that calling destructured
toStringfunction can cause issues withthis.