From 8d00075765b998532e0506dafa72be1fd1ae801c Mon Sep 17 00:00:00 2001 From: Per Date: Tue, 14 Apr 2020 13:06:01 +0200 Subject: [PATCH] improve error handling when files passed as arguments at launch cannot be opened --- src/main/index.ts | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/main/index.ts b/src/main/index.ts index 80f1138..8985700 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -286,16 +286,31 @@ function createWindow() { const openCommand = process.argv[argCount - 2] const filePath = process.argv[argCount - 1] if (openCommand == 'open' && filePath) { - if (ProjectFile.isProjectFile(filePath)) { - window.webContents.send( - OpenProjectMessage.type, - new OpenProjectMessage(filePath, false) - ) - } else { - window.webContents.send( - OpenImageMessage.type, - new OpenImageMessage(filePath) - ) + try { + // Make sure the file can be opened before proceeding + const fd = openSync(filePath, 'r') + closeSync(fd) + + if (ProjectFile.isProjectFile(filePath)) { + window.webContents.send( + OpenProjectMessage.type, + new OpenProjectMessage(filePath, false) + ) + } else { + window.webContents.send( + OpenImageMessage.type, + new OpenImageMessage(filePath) + ) + } + } catch (error) { + console.log(error) + console.log('process.argv:') + console.log(process.argv) + + const errorMessage = 'Failed to open \'' + filePath + '\'. ' + error + dialog.showMessageBoxSync(window, { + message: errorMessage + }) } } }