From b6675853ed0b80becfb84348e8154f3493393c3d Mon Sep 17 00:00:00 2001 From: kangfenmao Date: Wed, 21 May 2025 22:38:57 +0800 Subject: [PATCH] fix: handle user cancellation and improve error reporting in file saving process - Added a check for user cancellation in the file save dialog, rejecting the promise if canceled. - Enhanced error handling to reject the promise with a detailed error message instead of returning null. --- src/main/services/FileStorage.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/services/FileStorage.ts b/src/main/services/FileStorage.ts index 697eb6dd7c..f055bdc5fb 100644 --- a/src/main/services/FileStorage.ts +++ b/src/main/services/FileStorage.ts @@ -328,7 +328,7 @@ class FileStorage { fileName: string, content: string, options?: SaveDialogOptions - ): Promise => { + ): Promise => { try { const result: SaveDialogReturnValue = await dialog.showSaveDialog({ title: '保存文件', @@ -336,14 +336,18 @@ class FileStorage { ...options }) + if (result.canceled) { + return Promise.reject(new Error('User canceled the save dialog')) + } + if (!result.canceled && result.filePath) { await writeFileSync(result.filePath, content, { encoding: 'utf-8' }) } return result.filePath - } catch (err) { + } catch (err: any) { logger.error('[IPC - Error]', 'An error occurred saving the file:', err) - return null + return Promise.reject('An error occurred saving the file: ' + err?.message) } }