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
This commit is contained in:
Benjin Dubishar
2023-05-18 15:23:31 -07:00
committed by GitHub
parent 89386c9c11
commit 4c579ca12b

View File

@@ -6,7 +6,7 @@
import * as vscode from 'vscode'; import * as vscode from 'vscode';
import * as path from 'path'; import * as path from 'path';
import * as constants from '../common/constants'; 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 { IConnectionInfo } from 'vscode-mssql';
import { defaultProjectNameFromDb, defaultProjectSaveLocation } from '../tools/newProjectTool'; import { defaultProjectNameFromDb, defaultProjectSaveLocation } from '../tools/newProjectTool';
import { ImportDataModel } from '../models/api/import'; import { ImportDataModel } from '../models/api/import';
@@ -30,21 +30,28 @@ export async function createNewProjectFromDatabaseWithQuickpick(connectionInfo?:
} }
let connectionUri: string = ''; let connectionUri: string = '';
let dbs: string[] | undefined = undefined; let dbs: string[] | undefined = undefined;
let isValidProfile = connectionProfile?.server !== undefined; // undefined when createProjectFromDatabase is launched without context (via command palette)
while (!dbs) { while (!dbs) {
// Get the list of databases now to validate that the connection is valid and re-prompt them if it isn't // Get the list of databases now to validate that the connection is valid and re-prompt them if it isn't
try { if (isValidProfile) {
connectionUri = await vscodeMssqlApi.connect(connectionProfile); try {
dbs = (await vscodeMssqlApi.listDatabases(connectionUri)) connectionUri = await vscodeMssqlApi.connect(connectionProfile);
.filter(db => !constants.systemDbs.includes(db)); // Filter out system dbs dbs = (await vscodeMssqlApi.listDatabases(connectionUri))
} catch (err) { .filter(db => !constants.systemDbs.includes(db)); // Filter out system dbs
// The mssql extension handles showing the error to the user. Prompt the user } catch (err) {
// for a new connection and then go and try getting the DBs again // 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); connectionProfile = await vscodeMssqlApi.promptForConnection(true);
if (!connectionProfile) { if (!connectionProfile) {
// User cancelled return undefined; // cancelled by user
return undefined; } else {
isValidProfile = true;
} }
} }
} }