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

View File

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

View File

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

View File

@@ -48,6 +48,9 @@ class App extends Component {
AllowDownload: false, AllowDownload: false,
AutoMountChecked: false, AutoMountChecked: false,
ConfigStorageType: null, ConfigStorageType: null,
DisplayError: false,
Error: null,
ErrorAction: null,
DownloadActive: false, DownloadActive: false,
DownloadProgress: 0.0, DownloadProgress: 0.0,
DownloadingDependency: false, DownloadingDependency: false,
@@ -280,27 +283,35 @@ class App extends Component {
notifyMountsBusy = (busy) => { notifyMountsBusy = (busy) => {
this.setState({MountsBusy: busy}) this.setState({MountsBusy: busy})
}; };
onCheckInstalledReply = (event, arg) => { onCheckInstalledReply = (event, arg) => {
const repertoryVersion = arg.data.Success && arg.data.Exists ? arg.data.Version : 'none'; const action = () => {
let versionAvailable = false; const repertoryVersion = arg.data.Success && arg.data.Exists ? arg.data.Version : 'none';
let versionAvailable = false;
if (repertoryVersion !== 'none') { if (repertoryVersion !== 'none') {
const latestVersion = this.state.VersionLookup[this.state.ReleaseTypes[this.state.Release]].length - 1; const latestVersion = this.state.VersionLookup[this.state.ReleaseTypes[this.state.Release]].length - 1;
let version = this.state.Version; let version = this.state.Version;
if (version === -1) { if (version === -1) {
version = latestVersion; version = latestVersion;
}
versionAvailable = version !== latestVersion;
} }
versionAvailable = version !== latestVersion;
}
this.setState({ this.setState({
AllowDownload: true, AllowDownload: true,
DownloadingDependency: false, DownloadingDependency: false,
MissingDependencies: arg.data.Dependencies, MissingDependencies: arg.data.Dependencies,
RepertoryVersion: repertoryVersion, RepertoryVersion: repertoryVersion,
VersionAvailable: versionAvailable, VersionAvailable: versionAvailable,
}); });
};
if (arg.data.Success) {
action();
} else {
this.setErrorState(arg.data.Error, action);
}
}; };
onDownloadFileComplete = (event, arg) => { onDownloadFileComplete = (event, arg) => {
@@ -506,6 +517,14 @@ class App extends Component {
} }
}; };
setErrorState = (error, action) => {
this.setState({
DisplayError: true,
Error: error,
ErrorAction: action,
});
};
updateCheckScheduledJob = () => { updateCheckScheduledJob = () => {
if (this.state.Platform !== 'unknown') { if (this.state.Platform !== 'unknown') {
if (ipcRenderer) { if (ipcRenderer) {
@@ -537,12 +556,18 @@ class App extends Component {
this.state.ConfigStorageType && this.state.ConfigStorageType &&
allowConfig; allowConfig;
const showUpgrade = !showConfig && const showUpgrade = !this.state.DisplayError &&
!showConfig &&
!missingDependencies && !missingDependencies &&
!this.state.DownloadActive && !this.state.DownloadActive &&
this.state.UpgradeAvailable && this.state.UpgradeAvailable &&
!this.state.UpgradeDismissed; !this.state.UpgradeDismissed;
let errorDisplay = null;
if (this.state.DisplayError) {
}
let configDisplay = null; let configDisplay = null;
if (showConfig) { if (showConfig) {
configDisplay = ( configDisplay = (
@@ -664,6 +689,7 @@ class App extends Component {
return ( return (
<div styleName='App'> <div styleName='App'>
{errorDisplay}
{dependencyDisplay} {dependencyDisplay}
{upgradeDisplay} {upgradeDisplay}
{downloadDisplay} {downloadDisplay}

View File

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