diff --git a/packages/web/src/App.svelte b/packages/web/src/App.svelte index 65fa8f18b..60c34a6ec 100644 --- a/packages/web/src/App.svelte +++ b/packages/web/src/App.svelte @@ -3,6 +3,7 @@ import PluginsProvider from './plugins/PluginsProvider.svelte'; import Screen from './Screen.svelte'; + import './utility/errorHandler'; diff --git a/packages/web/src/utility/errorHandler.ts b/packages/web/src/utility/errorHandler.ts new file mode 100644 index 000000000..00f687e58 --- /dev/null +++ b/packages/web/src/utility/errorHandler.ts @@ -0,0 +1,30 @@ +import localforage from 'localforage'; + +window.onunhandledrejection = async e => { + console.log('Unhandler error, CRASH!!!', e); + const lastDbGateCrashJson = localStorage.getItem('lastDbGateCrash'); + const lastDbGateCrash = lastDbGateCrashJson ? JSON.parse(lastDbGateCrashJson) : null; + + if (lastDbGateCrash && new Date().getTime() - lastDbGateCrash < 30 * 1000) { + if ( + window.confirm( + 'Sorry, DbGate has crashed again.\nDo you want to clear local user data to avoid crashing after next reload?' + ) + ) { + localStorage.clear(); + try { + await localforage.clear(); + } catch (err) { + console.error('Error clearing app data', err); + } + window.location.reload(); + } else { + localStorage.setItem('lastDbGateCrash', JSON.stringify(new Date().getTime())); + window.location.reload(); + } + } else { + window.alert('Sorry, DbGate has crashed.\nAfter OK DbGate will be reloaded'); + localStorage.setItem('lastDbGateCrash', JSON.stringify(new Date().getTime())); + window.location.reload(); + } +}; diff --git a/packages/web/src/utility/uploadFiles.ts b/packages/web/src/utility/uploadFiles.ts index b60622de4..c067b16e9 100644 --- a/packages/web/src/utility/uploadFiles.ts +++ b/packages/web/src/utility/uploadFiles.ts @@ -6,6 +6,7 @@ import resolveApi from './resolveApi'; import { findFileFormat } from '../plugins/fileformats'; import { showModal } from '../modals/modalTools'; import ImportExportModal from '../modals/ImportExportModal.svelte'; +import ErrorMessageModal from '../modals/ErrorMessageModal.svelte'; let uploadListener; @@ -21,15 +22,20 @@ export default function uploadFiles(files) { const ext = get(extensions); const electron = getElectron(); files.forEach(async file => { - if (parseInt(file.size, 10) >= 4 * 1024 * 1024) { - // to big file + if (electron && canOpenByElectron(file.path, ext)) { + openElectronFileCore(file.path, ext); return; } - console.log('FILE', file); - - if (electron && canOpenByElectron(file.path, ext)) { - openElectronFileCore(file.path, ext); + const maxSize = 32 * 1024 * 1024; + if (parseInt(file.size, 10) >= maxSize) { + showModal(ErrorMessageModal, { + title: 'Upload error', + message: `File is too big, current size is ${Math.round( + file.size / 1024 + ).toLocaleString()} KB, max allowed size is ${Math.round(maxSize / 1024).toLocaleString()} KB`, + }); + // to big file return; }