added better error handling

This commit is contained in:
Abhinav 2022-06-27 07:13:33 +05:30
parent 97696d84f1
commit f8fc8a926c
2 changed files with 114 additions and 68 deletions

View file

@ -31,6 +31,7 @@ import OverflowMenu from './OverflowMenu/menu';
import { OverflowMenuOption } from './OverflowMenu/option'; import { OverflowMenuOption } from './OverflowMenu/option';
import { useLocalState } from 'hooks/useLocalState'; import { useLocalState } from 'hooks/useLocalState';
import { convertBytesToHumanReadable } from 'utils/billing'; import { convertBytesToHumanReadable } from 'utils/billing';
import { CustomError } from 'utils/error';
const ExportFolderPathContainer = styled('span')` const ExportFolderPathContainer = styled('span')`
white-space: nowrap; white-space: nowrap;
@ -179,11 +180,7 @@ export default function ExportModal(props: Props) {
const preExportRun = async () => { const preExportRun = async () => {
const exportFolder = getData(LS_KEYS.EXPORT)?.folder; const exportFolder = getData(LS_KEYS.EXPORT)?.folder;
if (!exportFolder) { if (!exportFolder) {
const folderSelected = await selectExportDirectory(); await selectExportDirectory();
if (!folderSelected) {
// no-op as select folder aborted
return;
}
} }
updateExportStage(ExportStage.INPROGRESS); updateExportStage(ExportStage.INPROGRESS);
await sleep(100); await sleep(100);
@ -193,10 +190,32 @@ export default function ExportModal(props: Props) {
updateExportStage(ExportStage.FINISHED); updateExportStage(ExportStage.FINISHED);
await sleep(100); await sleep(100);
updateExportTime(Date.now()); updateExportTime(Date.now());
syncExportStatsWithReport(); syncExportStatsWithRecord();
} }
}; };
const selectExportDirectory = async () => {
const newFolder = await exportService.selectExportDirectory();
if (newFolder) {
updateExportFolder(newFolder);
} else {
throw Error(CustomError.REQUEST_CANCELLED);
}
};
const syncExportStatsWithRecord = async () => {
const exportRecord = await exportService.getExportRecord();
const failed = exportRecord?.failedFiles?.length ?? 0;
const success = exportRecord?.exportedFiles?.length ?? 0;
setExportStats({ failed, success });
};
// =============
// UI functions
// =============
const startExport = async () => { const startExport = async () => {
try {
await preExportRun(); await preExportRun();
updateExportProgress({ current: 0, total: 0 }); updateExportProgress({ current: 0, total: 0 });
const exportResult = await exportService.exportFiles( const exportResult = await exportService.exportFiles(
@ -204,20 +223,38 @@ export default function ExportModal(props: Props) {
ExportType.NEW ExportType.NEW
); );
await postExportRun(exportResult); await postExportRun(exportResult);
} catch (e) {
if (e.message !== CustomError.REQUEST_CANCELLED) {
logError(e, 'startExport failed');
}
}
}; };
const stopExport = async () => { const stopExport = async () => {
try {
exportService.stopRunningExport(); exportService.stopRunningExport();
postExportRun(); postExportRun();
} catch (e) {
if (e.message !== CustomError.REQUEST_CANCELLED) {
logError(e, 'stopExport failed');
}
}
}; };
const pauseExport = () => { const pauseExport = () => {
try {
updateExportStage(ExportStage.PAUSED); updateExportStage(ExportStage.PAUSED);
exportService.pauseRunningExport(); exportService.pauseRunningExport();
postExportRun({ paused: true }); postExportRun({ paused: true });
} catch (e) {
if (e.message !== CustomError.REQUEST_CANCELLED) {
logError(e, 'pauseExport failed');
}
}
}; };
const resumeExport = async () => { const resumeExport = async () => {
try {
const exportRecord = await exportService.getExportRecord(); const exportRecord = await exportService.getExportRecord();
await preExportRun(); await preExportRun();
@ -235,9 +272,15 @@ export default function ExportModal(props: Props) {
); );
await postExportRun(exportResult); await postExportRun(exportResult);
} catch (e) {
if (e.message !== CustomError.REQUEST_CANCELLED) {
logError(e, 'resumeExport failed');
}
}
}; };
const retryFailedExport = async () => { const retryFailedExport = async () => {
try {
await preExportRun(); await preExportRun();
updateExportProgress({ current: 0, total: exportStats.failed }); updateExportProgress({ current: 0, total: exportStats.failed });
@ -246,22 +289,10 @@ export default function ExportModal(props: Props) {
ExportType.RETRY_FAILED ExportType.RETRY_FAILED
); );
await postExportRun(exportResult); await postExportRun(exportResult);
}; } catch (e) {
if (e.message !== CustomError.REQUEST_CANCELLED) {
const syncExportStatsWithReport = async () => { logError(e, 'retryFailedExport failed');
const exportRecord = await exportService.getExportRecord(); }
const failed = exportRecord?.failedFiles?.length ?? 0;
const success = exportRecord?.exportedFiles?.length ?? 0;
setExportStats({ failed, success });
};
const selectExportDirectory = async () => {
const newFolder = await exportService.selectExportDirectory();
if (newFolder) {
updateExportFolder(newFolder);
return true;
} else {
return false;
} }
}; };
@ -373,6 +404,15 @@ function ExportSize({ exportSize }) {
} }
function ExportDirectoryOption({ selectExportDirectory }) { function ExportDirectoryOption({ selectExportDirectory }) {
const handleClick = () => {
try {
selectExportDirectory();
} catch (e) {
if (e.message !== CustomError.REQUEST_CANCELLED) {
logError(e, 'startExport failed');
}
}
};
return ( return (
<OverflowMenu <OverflowMenu
triggerButtonProps={{ triggerButtonProps={{
@ -383,7 +423,7 @@ function ExportDirectoryOption({ selectExportDirectory }) {
ariaControls={'export-option'} ariaControls={'export-option'}
triggerButtonIcon={<MoreHoriz />}> triggerButtonIcon={<MoreHoriz />}>
<OverflowMenuOption <OverflowMenuOption
onClick={selectExportDirectory} onClick={handleClick}
startIcon={<FolderIcon />}> startIcon={<FolderIcon />}>
{constants.CHANGE_FOLDER} {constants.CHANGE_FOLDER}
</OverflowMenuOption> </OverflowMenuOption>

View file

@ -69,8 +69,14 @@ class ExportService {
this.allElectronAPIsExist = !!this.ElectronAPIs?.exists; this.allElectronAPIsExist = !!this.ElectronAPIs?.exists;
} }
async selectExportDirectory() { async selectExportDirectory() {
try {
return await this.ElectronAPIs.selectRootDirectory(); return await this.ElectronAPIs.selectRootDirectory();
} catch (e) {
logError(e, 'failed to selectExportDirectory ');
throw e;
} }
}
stopRunningExport() { stopRunningExport() {
this.stopExport = true; this.stopExport = true;
} }