From 5f66cfe5d5b3b373c4d0be383c4dc93b8298a5cd Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Mon, 3 Apr 2023 15:13:06 +0530 Subject: [PATCH] fix(ux): prevent unable to acquire conn --- src/App.vue | 53 +++++++++++++++++++++++++++++++++++++++++++------ src/utils/db.ts | 39 ++++++++++++++++++++++++++++++------ 2 files changed, 80 insertions(+), 12 deletions(-) diff --git a/src/App.vue b/src/App.vue index fc1471ff..fcc14f17 100644 --- a/src/App.vue +++ b/src/App.vue @@ -50,7 +50,11 @@ import SetupWizard from './pages/SetupWizard/SetupWizard.vue'; import setupInstance from './setup/setupInstance'; import { SetupWizardOptions } from './setup/types'; import './styles/index.css'; -import { connectToDatabase, dbErrorActionSymbols } from './utils/db'; +import { + connectToDatabase, + dbErrorActionSymbols, + handleDatabaseConnectionError, +} from './utils/db'; import { initializeInstance } from './utils/initialization'; import * as injectionKeys from './utils/injectionKeys'; import { checkForUpdates } from './utils/ipcCalls'; @@ -191,20 +195,57 @@ export default defineComponent({ return await this.handleConnectionFailed(error, actionSymbol); } - const setupComplete = await fyo.getValue( - ModelNameEnum.AccountingSettings, - 'setupComplete' - ); + const setupComplete = await this.getSetupComplete(filePath); + if (typeof setupComplete === 'object') { + return await this.handleConnectionFailed( + setupComplete.error, + setupComplete.actionSymbol + ); + } if (!setupComplete) { this.activeScreen = Screen.SetupWizard; return; } - await initializeInstance(filePath, false, countryCode, fyo); + await this.initializeInstanceWithErrorHandling(filePath, countryCode); await updatePrintTemplates(fyo); await this.setDesk(filePath); }, + async getSetupComplete(dbPath: string) { + try { + return (await fyo.getValue( + ModelNameEnum.AccountingSettings, + 'setupComplete' + )) as boolean; + } catch (error) { + if (!(error instanceof Error)) { + throw error; + } + + return { + error, + actionSymbol: await handleDatabaseConnectionError(error, dbPath), + }; + } + }, + async initializeInstanceWithErrorHandling( + dbPath: string, + countryCode: string + ) { + try { + return await initializeInstance(dbPath, false, countryCode, fyo); + } catch (error) { + if (!(error instanceof Error)) { + throw error; + } + + this.handleConnectionFailed( + error, + await handleDatabaseConnectionError(error, dbPath) + ); + } + }, async handleConnectionFailed(error: Error, actionSymbol: symbol) { await this.showDbSelector(); diff --git a/src/utils/db.ts b/src/utils/db.ts index 16078f93..521df283 100644 --- a/src/utils/db.ts +++ b/src/utils/db.ts @@ -7,6 +7,7 @@ export const dbErrorActionSymbols = { const dbErrors = { DirectoryDoesNotExist: 'directory does not exist', + UnableToAcquireConnection: 'Unable to acquire a connection', } as const; type Conn = { @@ -27,26 +28,52 @@ export async function connectToDatabase( throw error; } - const actionSymbol = await handleDatabaseConnectionError(error, dbPath); - - return { countryCode: '', error, actionSymbol }; + return { + countryCode: '', + error, + actionSymbol: await handleDatabaseConnectionError(error, dbPath), + }; } } -async function handleDatabaseConnectionError(error: Error, dbPath: string) { - if (error.message?.includes(dbErrors.DirectoryDoesNotExist)) { +export async function handleDatabaseConnectionError( + error: Error, + dbPath: string +) { + const message = error.message; + if (typeof message !== 'string') { + throw error; + } + + if (message.includes(dbErrors.DirectoryDoesNotExist)) { return await handleDirectoryDoesNotExist(dbPath); } + if (message.includes(dbErrors.UnableToAcquireConnection)) { + return await handleUnableToAcquireConnection(dbPath); + } + throw error; } +async function handleUnableToAcquireConnection(dbPath: string) { + return await showDbErrorDialog( + t`Could not connect to database file ${dbPath}, please select the file manually` + ); +} + async function handleDirectoryDoesNotExist(dbPath: string) { + return await showDbErrorDialog( + t`Directory for database file ${dbPath} does not exist, please select the file manually` + ); +} + +async function showDbErrorDialog(detail: string) { const { showDialog } = await import('src/utils/interactive'); return await showDialog({ type: 'error', title: t`Cannot Open File`, - detail: t`Directory for file ${dbPath} does not exist`, + detail, buttons: [ { label: t`Select File`,