[Partial error handling changes] [Standardize IPC replies]

This commit is contained in:
Scott E. Graves
2018-10-02 18:43:02 -05:00
parent 51ca79fd4f
commit 7e82690f5e
5 changed files with 136 additions and 160 deletions

View File

@@ -26,6 +26,7 @@ function createWindow() {
width: 425,
height: height,
resizable: false,
title: 'Repertory UI',
webPreferences: {
webSecurity: !process.env.ELECTRON_START_URL
}
@@ -138,6 +139,16 @@ if (!instanceLock) {
});
}
const standardIPCReply = (event, channel, data, error) => {
event.sender.send(channel, {
data: {
...data,
Error: error,
Success: !error,
}
});
};
ipcMain.on(Constants.IPC_Check_Installed, (event, data) => {
const dataDirectory = helpers.resolvePath(data.Directory);
const destination = path.join(dataDirectory, data.Version);
@@ -149,23 +160,16 @@ ipcMain.on(Constants.IPC_Check_Installed, (event, data) => {
exists = fs.existsSync(destination) && fs.lstatSync(destination).isDirectory();
} catch (e) {
}
event.sender.send(Constants.IPC_Check_Installed_Reply, {
data: {
Dependencies: dependencies,
Exists: exists,
Success: true,
Version: data.Version,
}
});
}).catch((e) => {
event.sender.send(Constants.IPC_Check_Installed_Reply, {
data: {
Dependencies: [],
Error: e,
Success: false,
Version: data.Version,
}
standardIPCReply(event, Constants.IPC_Check_Installed_Reply, {
Dependencies: dependencies,
Exists: exists,
Version: data.Version,
});
}).catch(error => {
standardIPCReply(event, Constants.IPC_Check_Installed_Reply, {
Dependencies: [],
Version: data.Version,
}, error);
});
});
@@ -226,31 +230,23 @@ ipcMain.on(Constants.IPC_Detect_Mounts, (event, data) => {
siaLocation = siaLocation.toUpperCase();
grabDriveLetters(hsLocation, siaLocation);
}
event.sender.send(Constants.IPC_Detect_Mounts_Reply, {
data: {
DriveLetters: driveLetters,
Locations: {
Hyperspace: hsLocation,
Sia: siaLocation,
},
Success: true,
PIDS: {
Hyperspace: results.Hyperspace.PID,
Sia: results.Sia.PID,
}
standardIPCReply(event, Constants.IPC_Detect_Mounts_Reply, {
DriveLetters: driveLetters,
Locations: {
Hyperspace: hsLocation,
Sia: siaLocation,
},
PIDS: {
Hyperspace: results.Hyperspace.PID,
Sia: results.Sia.PID,
}
});
})
.catch((err) => {
.catch(error => {
grabDriveLetters('', '');
event.sender.send(Constants.IPC_Detect_Mounts_Reply, {
data: {
DriveLetters: driveLetters,
Error: err,
Success: false,
}
});
standardIPCReply(event, Constants.IPC_Detect_Mounts_Reply, {
DriveLetters: driveLetters,
}, error);
});
});
@@ -258,22 +254,16 @@ ipcMain.on(Constants.IPC_Download_File, (event, data) => {
const dataDirectory = helpers.resolvePath(data.Directory);
const destination = path.join(dataDirectory, data.Filename);
helpers.downloadFile(data.URL, destination, (progress) => {
event.sender.send(Constants.IPC_Download_File_Progress, {
data: {
Destination: destination,
Progress: progress,
URL: data.URL,
}
});
}, (success, err) => {
event.sender.send(Constants.IPC_Download_File_Complete, {
data: {
Destination: destination,
Error: err,
Success: success,
URL: data.URL,
}
standardIPCReply(event, Constants.IPC_Download_File_Progress, {
Destination: destination,
Progress: progress,
URL: data.URL,
});
}, error => {
standardIPCReply(event, Constants.IPC_Download_File_Complete, {
Destination: destination,
URL: data.URL,
}, error);
});
});
@@ -285,27 +275,20 @@ ipcMain.on(Constants.IPC_Extract_Release, (event, data) => {
const stream = fs.createReadStream(data.Source);
stream
.pipe(unzip.Extract({ path: destination }))
.on('error', (e) => {
.on('error', error => {
try {
helpers.removeDirectoryRecursively(destination);
} catch (e) {
}
stream.close();
event.sender.send(Constants.IPC_Extract_Release_Complete, {
data: {
Error: e,
Source: data.Source,
Success: false,
}
});
standardIPCReply(event, Constants.IPC_Extract_Release_Complete, {
Source: data.Source,
}, error);
})
.on('finish', () => {
stream.close();
event.sender.send(Constants.IPC_Extract_Release_Complete, {
data: {
Source: data.Source,
Success: true,
}
standardIPCReply(event, Constants.IPC_Extract_Release_Complete, {
Source: data.Source,
});
});
});
@@ -316,28 +299,15 @@ ipcMain.on(Constants.IPC_Get_Config, (event, data) => {
.getConfig(dataDirectory, data.Version, data.StorageType)
.then((data) => {
if (data.Code === 0) {
event.sender.send(Constants.IPC_Get_Config_Reply, {
data: {
Success: true,
Config: data.Data,
}
standardIPCReply(event, Constants.IPC_Get_Config_Reply, {
Config: data.Data,
});
} else {
event.sender.send(Constants.IPC_Get_Config_Reply, {
data: {
Error: data.Code,
Success: false,
}
});
standardIPCReply(event, Constants.IPC_Get_Config_Reply, {}, data.Code);
}
})
.catch((e)=> {
event.sender.send(Constants.IPC_Get_Config_Reply, {
data: {
Error: e,
Success: false,
}
});
.catch(error => {
standardIPCReply(event, Constants.IPC_Get_Config_Reply, {}, error);
});
});
@@ -346,20 +316,12 @@ ipcMain.on(Constants.IPC_Get_Config_Template, (event, data) => {
helpers
.getConfigTemplate(dataDirectory, data.Version, data.StorageType)
.then((data) => {
event.sender.send(Constants.IPC_Get_Config_Template_Reply, {
data: {
Success: true,
Template: data,
}
standardIPCReply(event, Constants.IPC_Get_Config_Template_Reply, {
Template: data,
});
})
.catch((e)=> {
event.sender.send(Constants.IPC_Get_Config_Template_Reply, {
data: {
Error: e,
Success: false,
}
});
.catch(error => {
standardIPCReply(event, Constants.IPC_Get_Config_Template_Reply, {}, error);
});
});
@@ -385,32 +347,25 @@ ipcMain.on(Constants.IPC_Get_State, (event, data) => {
});
ipcMain.on(Constants.IPC_Grab_Releases, (event) => {
event.sender.send(Constants.IPC_Grab_Releases_Reply);
standardIPCReply(event, Constants.IPC_Grab_Releases_Reply);
});
ipcMain.on(Constants.IPC_Grab_UI_Releases, (event) => {
event.sender.send(Constants.IPC_Grab_UI_Releases_Reply);
standardIPCReply(event, Constants.IPC_Grab_UI_Releases_Reply);
});
ipcMain.on(Constants.IPC_Install_Dependency, (event, data) => {
helpers
.executeAndWait(data.Source)
.then(()=> {
event.sender.send(Constants.IPC_Install_Dependency_Reply, {
data: {
Source: data.Source,
Success: true,
}
standardIPCReply(event, Constants.IPC_Install_Dependency_Reply, {
Source: data.Source,
});
})
.catch((e)=> {
event.sender.send(Constants.IPC_Install_Dependency_Reply, {
data: {
Error: e,
Source: data.Source,
Success: false,
}
});
.catch(error => {
standardIPCReply(event, Constants.IPC_Install_Dependency_Reply, {
Source: data.Source,
}, error);
});
});
@@ -420,46 +375,36 @@ ipcMain.on(Constants.IPC_Install_Upgrade, (event, data) => {
.then(()=> {
mainWindow.close();
})
.catch((e)=> {
event.sender.send(Constants.IPC_Install_Upgrade_Reply, {
data: {
Error: e,
Source: data.Source,
Success: false,
}
});
.catch(error => {
standardIPCReply(event, Constants.IPC_Install_Upgrade_Reply, {
Source: data.Source,
}, error);
});
});
ipcMain.on(Constants.IPC_Mount_Drive, (event, data) => {
const dataDirectory = helpers.resolvePath(data.Directory);
const errorHandler = (pid) => {
const errorHandler = (pid, error) => {
mountedPIDs.splice(mountedPIDs.indexOf(pid), 1);
event.sender.send(Constants.IPC_Unmount_Drive_Reply, {
data: {
PID: -1,
StorageType: data.StorageType,
Success: false,
}
});
standardIPCReply(event, Constants.IPC_Unmount_Drive_Reply, {
PID: -1,
StorageType: data.StorageType,
}, error || Error(data.StorageType + ' Unmounted'));
};
helpers.executeMount(dataDirectory, data.Version, data.StorageType, data.Location, (_, pid)=> {
errorHandler(pid);
helpers.executeMount(dataDirectory, data.Version, data.StorageType, data.Location, (error, pid) => {
errorHandler(pid, error);
})
.then(pid=> {
.then(pid => {
if (pid !== -1) {
mountedPIDs.push(pid);
}
event.sender.send(Constants.IPC_Mount_Drive_Reply, {
data: {
PID: pid,
StorageType: data.StorageType,
Success: true,
}
standardIPCReply(event, Constants.IPC_Mount_Drive_Reply, {
PID: pid,
StorageType: data.StorageType,
});
})
.catch((_, pid) => {
errorHandler(pid);
.catch(error => {
errorHandler(-1, error);
});
});
@@ -483,7 +428,7 @@ ipcMain.on(Constants.IPC_Set_Config_Values, (event, data) => {
setConfigValue(++i);
});
} else {
event.sender.send(Constants.IPC_Set_Config_Values_Reply, {});
standardIPCReply(event, Constants.IPC_Set_Config_Values_Reply, {});
}
};
setConfigValue(0);