diff --git a/web/database/utils/store.js b/web/database/utils/store.js --- a/web/database/utils/store.js +++ b/web/database/utils/store.js @@ -57,7 +57,9 @@ const { draftStoreOperations, threadStoreOperations, reportStoreOperations } = storeOperations; - const convertedThreadStoreOperations = canUseDatabaseOnWeb(userID) + const canUseDatabase = canUseDatabaseOnWeb(userID); + + const convertedThreadStoreOperations = canUseDatabase ? threadStoreOpsHandlers.convertOpsToClientDBOps(threadStoreOperations) : []; const convertedReportStoreOperations = @@ -68,14 +70,23 @@ if (!isSupported) { return; } - await databaseModule.schedule({ - type: workerRequestMessageTypes.PROCESS_STORE_OPERATIONS, - storeOperations: { - draftStoreOperations, - reportStoreOperations: convertedReportStoreOperations, - threadStoreOperations: convertedThreadStoreOperations, - }, - }); + try { + await databaseModule.schedule({ + type: workerRequestMessageTypes.PROCESS_STORE_OPERATIONS, + storeOperations: { + draftStoreOperations, + reportStoreOperations: convertedReportStoreOperations, + threadStoreOperations: convertedThreadStoreOperations, + }, + }); + } catch (e) { + console.log(e); + if (canUseDatabase) { + window.alert(e.message); + await databaseModule.init({ clearDatabase: true }); + location.reload(); + } + } } export { getClientStore, processDBStoreOperations }; diff --git a/web/database/worker/process-operations.js b/web/database/worker/process-operations.js --- a/web/database/worker/process-operations.js +++ b/web/database/worker/process-operations.js @@ -10,6 +10,7 @@ ClientDBStore, ClientDBStoreOperations, } from 'lib/types/store-ops-types.js'; +import { getMessageForException } from 'lib/utils/errors.js'; import { clientDBThreadInfoToWebThread, @@ -22,16 +23,24 @@ operations: $ReadOnlyArray, ) { for (const operation: DraftStoreOperation of operations) { - if (operation.type === 'remove_all') { - sqliteQueryExecutor.removeAllDrafts(); - } else if (operation.type === 'update') { - const { key, text } = operation.payload; - sqliteQueryExecutor.updateDraft(key, text); - } else if (operation.type === 'move') { - const { oldKey, newKey } = operation.payload; - sqliteQueryExecutor.moveDraft(oldKey, newKey); - } else { - throw new Error('Unsupported draft operation'); + try { + if (operation.type === 'remove_all') { + sqliteQueryExecutor.removeAllDrafts(); + } else if (operation.type === 'update') { + const { key, text } = operation.payload; + sqliteQueryExecutor.updateDraft(key, text); + } else if (operation.type === 'move') { + const { oldKey, newKey } = operation.payload; + sqliteQueryExecutor.moveDraft(oldKey, newKey); + } else { + throw new Error('Unsupported draft operation'); + } + } catch (e) { + throw new Error( + `Error while processing draft operation: ${operation.type}: ${ + getMessageForException(e) ?? 'unknown error' + }`, + ); } } } @@ -41,16 +50,24 @@ operations: $ReadOnlyArray, ) { for (const operation: ClientDBReportStoreOperation of operations) { - if (operation.type === 'remove_all_reports') { - sqliteQueryExecutor.removeAllReports(); - } else if (operation.type === 'remove_reports') { - const { ids } = operation.payload; - sqliteQueryExecutor.removeReports(ids); - } else if (operation.type === 'replace_report') { - const { id, report } = operation.payload; - sqliteQueryExecutor.replaceReport({ id, report }); - } else { - throw new Error('Unsupported report operation'); + try { + if (operation.type === 'remove_all_reports') { + sqliteQueryExecutor.removeAllReports(); + } else if (operation.type === 'remove_reports') { + const { ids } = operation.payload; + sqliteQueryExecutor.removeReports(ids); + } else if (operation.type === 'replace_report') { + const { id, report } = operation.payload; + sqliteQueryExecutor.replaceReport({ id, report }); + } else { + throw new Error('Unsupported report operation'); + } + } catch (e) { + throw new Error( + `Error while processing report operation: ${operation.type}: ${ + getMessageForException(e) ?? 'unknown error' + }`, + ); } } } @@ -60,17 +77,25 @@ operations: $ReadOnlyArray, ) { for (const operation: ClientDBThreadStoreOperation of operations) { - if (operation.type === 'remove_all') { - sqliteQueryExecutor.removeAllThreads(); - } else if (operation.type === 'remove') { - const { ids } = operation.payload; - sqliteQueryExecutor.removeThreads(ids); - } else if (operation.type === 'replace') { - sqliteQueryExecutor.replaceThreadWeb( - clientDBThreadInfoToWebThread(operation.payload), + try { + if (operation.type === 'remove_all') { + sqliteQueryExecutor.removeAllThreads(); + } else if (operation.type === 'remove') { + const { ids } = operation.payload; + sqliteQueryExecutor.removeThreads(ids); + } else if (operation.type === 'replace') { + sqliteQueryExecutor.replaceThreadWeb( + clientDBThreadInfoToWebThread(operation.payload), + ); + } else { + throw new Error('Unsupported thread operation'); + } + } catch (e) { + throw new Error( + `Error while processing thread operation: ${operation.type}: ${ + getMessageForException(e) ?? 'unknown error' + }`, ); - } else { - throw new Error('Unsupported thread operation'); } } } @@ -97,6 +122,7 @@ } catch (e) { sqliteQueryExecutor.rollbackTransaction(); console.log('Error while processing store ops: ', e); + throw e; } }