[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: {
standardIPCReply(event, Constants.IPC_Check_Installed_Reply, {
Dependencies: dependencies,
Exists: exists,
Success: true,
Version: data.Version,
}
});
}).catch((e) => {
event.sender.send(Constants.IPC_Check_Installed_Reply, {
data: {
}).catch(error => {
standardIPCReply(event, Constants.IPC_Check_Installed_Reply, {
Dependencies: [],
Error: e,
Success: false,
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: {
standardIPCReply(event, Constants.IPC_Detect_Mounts_Reply, {
DriveLetters: driveLetters,
Locations: {
Hyperspace: hsLocation,
Sia: siaLocation,
},
Success: true,
PIDS: {
Hyperspace: results.Hyperspace.PID,
Sia: results.Sia.PID,
}
}
});
})
.catch((err) => {
.catch(error => {
grabDriveLetters('', '');
event.sender.send(Constants.IPC_Detect_Mounts_Reply, {
data: {
standardIPCReply(event, Constants.IPC_Detect_Mounts_Reply, {
DriveLetters: driveLetters,
Error: err,
Success: false,
}
});
}, 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: {
standardIPCReply(event, Constants.IPC_Download_File_Progress, {
Destination: destination,
Progress: progress,
URL: data.URL,
}
});
}, (success, err) => {
event.sender.send(Constants.IPC_Download_File_Complete, {
data: {
}, error => {
standardIPCReply(event, Constants.IPC_Download_File_Complete, {
Destination: destination,
Error: err,
Success: success,
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,
standardIPCReply(event, Constants.IPC_Extract_Release_Complete, {
Source: data.Source,
Success: false,
}
});
}, error);
})
.on('finish', () => {
stream.close();
event.sender.send(Constants.IPC_Extract_Release_Complete, {
data: {
standardIPCReply(event, Constants.IPC_Extract_Release_Complete, {
Source: data.Source,
Success: true,
}
});
});
});
@@ -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,
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,
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: {
standardIPCReply(event, Constants.IPC_Install_Dependency_Reply, {
Source: data.Source,
Success: true,
}
});
})
.catch((e)=> {
event.sender.send(Constants.IPC_Install_Dependency_Reply, {
data: {
Error: e,
.catch(error => {
standardIPCReply(event, Constants.IPC_Install_Dependency_Reply, {
Source: data.Source,
Success: false,
}
});
}, 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,
.catch(error => {
standardIPCReply(event, Constants.IPC_Install_Upgrade_Reply, {
Source: data.Source,
Success: false,
}
});
}, 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: {
standardIPCReply(event, Constants.IPC_Unmount_Drive_Reply, {
PID: -1,
StorageType: data.StorageType,
Success: false,
}
});
}, 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 => {
if (pid !== -1) {
mountedPIDs.push(pid);
}
event.sender.send(Constants.IPC_Mount_Drive_Reply, {
data: {
standardIPCReply(event, Constants.IPC_Mount_Drive_Reply, {
PID: pid,
StorageType: data.StorageType,
Success: true,
}
});
})
.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);

View File

@@ -80,18 +80,18 @@ module.exports.downloadFile = (url, destination, progressCallback, completeCallb
response.data.on('end', () => {
stream.end(() => {
completeCallback(true);
completeCallback();
});
});
response.data.on('error', (e) => {
stream.end(() => {
completeCallback(false, e);
completeCallback(e);
});
});
})
.catch((e)=> {
completeCallback(false, e);
completeCallback(e);
});
};

View File

@@ -1,6 +1,10 @@
{
"Locations": {
"win32": {
"1.0.1": {
"hash": "",
"urls": []
},
"1.0.0": {
"hash": "",
"urls": [
@@ -11,6 +15,7 @@
},
"Versions": {
"win32": [
"1.0.1",
"1.0.0"
]
}

View File

@@ -48,6 +48,9 @@ class App extends Component {
AllowDownload: false,
AutoMountChecked: false,
ConfigStorageType: null,
DisplayError: false,
Error: null,
ErrorAction: null,
DownloadActive: false,
DownloadProgress: 0.0,
DownloadingDependency: false,
@@ -282,6 +285,7 @@ class App extends Component {
};
onCheckInstalledReply = (event, arg) => {
const action = () => {
const repertoryVersion = arg.data.Success && arg.data.Exists ? arg.data.Version : 'none';
let versionAvailable = false;
@@ -303,6 +307,13 @@ class App extends Component {
});
};
if (arg.data.Success) {
action();
} else {
this.setErrorState(arg.data.Error, action);
}
};
onDownloadFileComplete = (event, arg) => {
if (this.state.DownloadingRelease) {
if (arg.data.Success) {
@@ -506,6 +517,14 @@ class App extends Component {
}
};
setErrorState = (error, action) => {
this.setState({
DisplayError: true,
Error: error,
ErrorAction: action,
});
};
updateCheckScheduledJob = () => {
if (this.state.Platform !== 'unknown') {
if (ipcRenderer) {
@@ -537,12 +556,18 @@ class App extends Component {
this.state.ConfigStorageType &&
allowConfig;
const showUpgrade = !showConfig &&
const showUpgrade = !this.state.DisplayError &&
!showConfig &&
!missingDependencies &&
!this.state.DownloadActive &&
this.state.UpgradeAvailable &&
!this.state.UpgradeDismissed;
let errorDisplay = null;
if (this.state.DisplayError) {
}
let configDisplay = null;
if (showConfig) {
configDisplay = (
@@ -664,6 +689,7 @@ class App extends Component {
return (
<div styleName='App'>
{errorDisplay}
{dependencyDisplay}
{upgradeDisplay}
{downloadDisplay}

View File

@@ -277,7 +277,7 @@ class Configuration extends Component {
<h1 style={{width: '100%', textAlign: 'center'}}>{this.props.storageType + ' Configuration'}</h1>
<div style={{overflowY: 'auto', height: '90%'}}>
{objectItems}
<h1>Settings</h1>
{(configurationItems.length > 0) ? <h1>Settings</h1> : null}
{configurationItems}
</div>
</Box>