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 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;
}
}
}