From 4c579ca12ba7bc069d183f447398b20fabd0427f Mon Sep 17 00:00:00 2001 From: Benjin Dubishar Date: Thu, 18 May 2023 15:23:31 -0700 Subject: [PATCH] Validating connectionProfile for missing server before attempting to connect (#23159) * Validating connectionProfile for missing server before attempting to connect * rewriting the logic to be more clear * adding error log --- .../createProjectFromDatabaseQuickpick.ts | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/extensions/sql-database-projects/src/dialogs/createProjectFromDatabaseQuickpick.ts b/extensions/sql-database-projects/src/dialogs/createProjectFromDatabaseQuickpick.ts index 81d723f4a8..02c7c5f950 100644 --- a/extensions/sql-database-projects/src/dialogs/createProjectFromDatabaseQuickpick.ts +++ b/extensions/sql-database-projects/src/dialogs/createProjectFromDatabaseQuickpick.ts @@ -6,7 +6,7 @@ import * as vscode from 'vscode'; import * as path from 'path'; import * as constants from '../common/constants'; -import { exists, getVscodeMssqlApi, isValidBasenameErrorMessage, sanitizeStringForFilename } from '../common/utils'; +import { exists, getVscodeMssqlApi, isValidBasenameErrorMessage, sanitizeStringForFilename, getErrorMessage } from '../common/utils'; import { IConnectionInfo } from 'vscode-mssql'; import { defaultProjectNameFromDb, defaultProjectSaveLocation } from '../tools/newProjectTool'; import { ImportDataModel } from '../models/api/import'; @@ -30,21 +30,28 @@ export async function createNewProjectFromDatabaseWithQuickpick(connectionInfo?: } let connectionUri: string = ''; let dbs: string[] | undefined = undefined; + + let isValidProfile = connectionProfile?.server !== undefined; // undefined when createProjectFromDatabase is launched without context (via command palette) + while (!dbs) { // Get the list of databases now to validate that the connection is valid and re-prompt them if it isn't - try { - connectionUri = await vscodeMssqlApi.connect(connectionProfile); - dbs = (await vscodeMssqlApi.listDatabases(connectionUri)) - .filter(db => !constants.systemDbs.includes(db)); // Filter out system dbs - } catch (err) { - // The mssql extension handles showing the error to the user. Prompt the user - // for a new connection and then go and try getting the DBs again + if (isValidProfile) { + try { + connectionUri = await vscodeMssqlApi.connect(connectionProfile); + dbs = (await vscodeMssqlApi.listDatabases(connectionUri)) + .filter(db => !constants.systemDbs.includes(db)); // Filter out system dbs + } catch (err) { + // Prompt the user for a new connection and then go and try getting the DBs again + isValidProfile = false; + console.error(getErrorMessage(err)); + } + } else { connectionProfile = await vscodeMssqlApi.promptForConnection(true); if (!connectionProfile) { - // User cancelled - return undefined; + return undefined; // cancelled by user + } else { + isValidProfile = true; } - } }