show better error messages when parsing sqlproj (#17589)

* show better error messages when parsing sqlproj

* show error messages in console
This commit is contained in:
Kim Santiago
2021-11-04 16:43:31 -07:00
committed by GitHub
parent 00ff7a0000
commit a34b5a0db7
3 changed files with 137 additions and 70 deletions

View File

@@ -105,7 +105,7 @@ export const dataSourceDropdownTitle = localize('dataSourceDropdownTitle', "Data
export const noDataSourcesText = localize('noDataSourcesText', "No data sources in this project"); export const noDataSourcesText = localize('noDataSourcesText', "No data sources in this project");
export const loadProfilePlaceholderText = localize('loadProfilePlaceholderText', "Load profile..."); export const loadProfilePlaceholderText = localize('loadProfilePlaceholderText', "Load profile...");
export const profileReadError = (err: any) => localize('profileReadError', "Error loading the publish profile. {0}", utils.getErrorMessage(err)); export const profileReadError = (err: any) => localize('profileReadError', "Error loading the publish profile. {0}", utils.getErrorMessage(err));
export const sqlCmdTableLabel = localize('sqlCmdTableLabel', "SQLCMD Variables"); export const sqlCmdVariables = localize('sqlCmdTableLabel', "SQLCMD Variables");
export const sqlCmdVariableColumn = localize('sqlCmdVariableColumn', "Name"); export const sqlCmdVariableColumn = localize('sqlCmdVariableColumn', "Name");
export const sqlCmdValueColumn = localize('sqlCmdValueColumn', "Value"); export const sqlCmdValueColumn = localize('sqlCmdValueColumn', "Value");
export const loadSqlCmdVarsButtonTitle = localize('reloadValuesFromProjectButtonTitle', "Reload values from project"); export const loadSqlCmdVarsButtonTitle = localize('reloadValuesFromProjectButtonTitle', "Reload values from project");
@@ -286,7 +286,7 @@ export function unableToFindSqlCmdVariable(variableName: string) { return locali
export function unableToFindDatabaseReference(reference: string) { return localize('unableToFindReference', "Unable to find database reference {0}", reference); } export function unableToFindDatabaseReference(reference: string) { return localize('unableToFindReference', "Unable to find database reference {0}", reference); }
export function invalidGuid(guid: string) { return localize('invalidGuid', "Specified GUID is invalid: {0}", guid); } export function invalidGuid(guid: string) { return localize('invalidGuid', "Specified GUID is invalid: {0}", guid); }
export function invalidTargetPlatform(targetPlatform: string, supportedTargetPlatforms: string[]) { return localize('invalidTargetPlatform', "Invalid target platform: {0}. Supported target platforms: {1}", targetPlatform, supportedTargetPlatforms.toString()); } export function invalidTargetPlatform(targetPlatform: string, supportedTargetPlatforms: string[]) { return localize('invalidTargetPlatform', "Invalid target platform: {0}. Supported target platforms: {1}", targetPlatform, supportedTargetPlatforms.toString()); }
export function errorReadingProject(section: string, path: string) { return localize('errorReadingProjectGuid', "Error trying to read {0} of project '{1}'", section, path); }
// Action types // Action types
export const deleteAction = localize('deleteAction', 'Delete'); export const deleteAction = localize('deleteAction', 'Delete');
@@ -363,6 +363,16 @@ export const Type = 'Type';
export const ExternalStreamingJob: string = 'ExternalStreamingJob'; export const ExternalStreamingJob: string = 'ExternalStreamingJob';
export const Sdk: string = 'Sdk'; export const Sdk: string = 'Sdk';
export const BuildElements = localize('buildElements', "Build Elements");
export const FolderElements = localize('folderElements', "Folder Elements");
export const PreDeployElements = localize('preDeployElements', "PreDeploy Elements");
export const PostDeployElements = localize('postDeployElements', "PostDeploy Elements");
export const NoneElements = localize('noneElements', "None Elements");
export const ImportElements = localize('importElements', "Import Elements");
export const ProjectReferenceNameElement = localize('projectReferenceNameElement', "Project reference name element");
export const ProjectReferenceElement = localize('projectReferenceElement', "Project reference");
export const DacpacReferenceElement = localize('dacpacReferenceElement', "Dacpac reference");
/** Name of the property item in the project file that defines default database collation. */ /** Name of the property item in the project file that defines default database collation. */
export const DefaultCollationProperty = 'DefaultCollation'; export const DefaultCollationProperty = 'DefaultCollation';

View File

@@ -111,7 +111,7 @@ export class PublishDatabaseDialog {
component: <azdataType.DeclarativeTableComponent>this.sqlCmdVariablesTable component: <azdataType.DeclarativeTableComponent>this.sqlCmdVariablesTable
} }
], ],
title: constants.sqlCmdTableLabel title: constants.sqlCmdVariables
}; };
const profileRow = this.createProfileRow(view); const profileRow = this.createProfileRow(view);
@@ -423,7 +423,7 @@ export class PublishDatabaseDialog {
this.sqlCmdVars = { ...this.project.sqlCmdVariables }; this.sqlCmdVars = { ...this.project.sqlCmdVariables };
const table = view.modelBuilder.declarativeTable().withProps({ const table = view.modelBuilder.declarativeTable().withProps({
ariaLabel: constants.sqlCmdTableLabel, ariaLabel: constants.sqlCmdVariables,
dataValues: this.convertSqlCmdVarsToTableFormat(this.sqlCmdVars), dataValues: this.convertSqlCmdVarsToTableFormat(this.sqlCmdVars),
columns: [ columns: [
{ {

View File

@@ -120,18 +120,30 @@ export class Project implements ISqlProject {
// check if this is a new msbuild sdk style project // check if this is a new msbuild sdk style project
this._isMsbuildSdkStyleProject = this.CheckForMsbuildSdkStyleProject(); this._isMsbuildSdkStyleProject = this.CheckForMsbuildSdkStyleProject();
// get projectGUID // get projectGUID
try {
this._projectGuid = this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.ProjectGuid)[0].childNodes[0].nodeValue!; this._projectGuid = this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.ProjectGuid)[0].childNodes[0].nodeValue!;
} catch (e) {
void window.showErrorMessage(constants.errorReadingProject(constants.ProjectGuid, this.projectFilePath));
console.error(utils.getErrorMessage(e));
}
// find all folders and files to include // find all folders and files to include
for (let ig = 0; ig < this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.ItemGroup).length; ig++) { for (let ig = 0; ig < this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.ItemGroup).length; ig++) {
const itemGroup = this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.ItemGroup)[ig]; const itemGroup = this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.ItemGroup)[ig];
try {
const buildElements = itemGroup.getElementsByTagName(constants.Build); const buildElements = itemGroup.getElementsByTagName(constants.Build);
for (let b = 0; b < buildElements.length; b++) { for (let b = 0; b < buildElements.length; b++) {
this._files.push(this.createFileProjectEntry(buildElements[b].getAttribute(constants.Include)!, EntryType.File, buildElements[b].getAttribute(constants.Type)!)); this._files.push(this.createFileProjectEntry(buildElements[b].getAttribute(constants.Include)!, EntryType.File, buildElements[b].getAttribute(constants.Type)!));
} }
} catch (e) {
void window.showErrorMessage(constants.errorReadingProject(constants.BuildElements, this.projectFilePath));
console.error(utils.getErrorMessage(e));
}
try {
const folderElements = itemGroup.getElementsByTagName(constants.Folder); const folderElements = itemGroup.getElementsByTagName(constants.Folder);
for (let f = 0; f < folderElements.length; f++) { for (let f = 0; f < folderElements.length; f++) {
// don't add Properties folder since it isn't supported for now // don't add Properties folder since it isn't supported for now
@@ -139,47 +151,77 @@ export class Project implements ISqlProject {
this._files.push(this.createFileProjectEntry(folderElements[f].getAttribute(constants.Include)!, EntryType.Folder)); this._files.push(this.createFileProjectEntry(folderElements[f].getAttribute(constants.Include)!, EntryType.Folder));
} }
} }
} catch (e) {
void window.showErrorMessage(constants.errorReadingProject(constants.Folder, this.projectFilePath));
console.error(utils.getErrorMessage(e));
}
// find all pre-deployment scripts to include // find all pre-deployment scripts to include
let preDeployScriptCount: number = 0; let preDeployScriptCount: number = 0;
try {
const preDeploy = itemGroup.getElementsByTagName(constants.PreDeploy); const preDeploy = itemGroup.getElementsByTagName(constants.PreDeploy);
for (let pre = 0; pre < preDeploy.length; pre++) { for (let pre = 0; pre < preDeploy.length; pre++) {
this._preDeployScripts.push(this.createFileProjectEntry(preDeploy[pre].getAttribute(constants.Include)!, EntryType.File)); this._preDeployScripts.push(this.createFileProjectEntry(preDeploy[pre].getAttribute(constants.Include)!, EntryType.File));
preDeployScriptCount++; preDeployScriptCount++;
} }
} catch (e) {
void window.showErrorMessage(constants.errorReadingProject(constants.PreDeployElements, this.projectFilePath));
console.error(utils.getErrorMessage(e));
}
// find all post-deployment scripts to include // find all post-deployment scripts to include
let postDeployScriptCount: number = 0; let postDeployScriptCount: number = 0;
try {
const postDeploy = itemGroup.getElementsByTagName(constants.PostDeploy); const postDeploy = itemGroup.getElementsByTagName(constants.PostDeploy);
for (let post = 0; post < postDeploy.length; post++) { for (let post = 0; post < postDeploy.length; post++) {
this._postDeployScripts.push(this.createFileProjectEntry(postDeploy[post].getAttribute(constants.Include)!, EntryType.File)); this._postDeployScripts.push(this.createFileProjectEntry(postDeploy[post].getAttribute(constants.Include)!, EntryType.File));
postDeployScriptCount++; postDeployScriptCount++;
} }
} catch (e) {
void window.showErrorMessage(constants.errorReadingProject(constants.PostDeployElements, this.projectFilePath));
console.error(utils.getErrorMessage(e));
}
if (preDeployScriptCount > 1 || postDeployScriptCount > 1) { if (preDeployScriptCount > 1 || postDeployScriptCount > 1) {
void window.showWarningMessage(constants.prePostDeployCount, constants.okString); void window.showWarningMessage(constants.prePostDeployCount, constants.okString);
} }
// find all none-deployment scripts to include // find all none-deployment scripts to include
try {
const noneItems = itemGroup.getElementsByTagName(constants.None); const noneItems = itemGroup.getElementsByTagName(constants.None);
for (let n = 0; n < noneItems.length; n++) { for (let n = 0; n < noneItems.length; n++) {
this._noneDeployScripts.push(this.createFileProjectEntry(noneItems[n].getAttribute(constants.Include)!, EntryType.File)); this._noneDeployScripts.push(this.createFileProjectEntry(noneItems[n].getAttribute(constants.Include)!, EntryType.File));
} }
} catch (e) {
void window.showErrorMessage(constants.errorReadingProject(constants.NoneElements, this.projectFilePath));
console.error(utils.getErrorMessage(e));
}
} }
// find all import statements to include // find all import statements to include
try {
const importElements = this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.Import); const importElements = this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.Import);
for (let i = 0; i < importElements.length; i++) { for (let i = 0; i < importElements.length; i++) {
const importTarget = importElements[i]; const importTarget = importElements[i];
this._importedTargets.push(importTarget.getAttribute(constants.Project)!); this._importedTargets.push(importTarget.getAttribute(constants.Project)!);
} }
} catch (e) {
void window.showErrorMessage(constants.errorReadingProject(constants.ImportElements, this.projectFilePath));
console.error(utils.getErrorMessage(e));
}
// find all SQLCMD variables to include // find all SQLCMD variables to include
try {
this._sqlCmdVariables = utils.readSqlCmdVariables(this.projFileXmlDoc, false); this._sqlCmdVariables = utils.readSqlCmdVariables(this.projFileXmlDoc, false);
} catch (e) {
void window.showErrorMessage(constants.errorReadingProject(constants.sqlCmdVariables, this.projectFilePath));
console.error(utils.getErrorMessage(e));
}
// find all database references to include // find all database references to include
const references = this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.ArtifactReference); const references = this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.ArtifactReference);
for (let r = 0; r < references.length; r++) { for (let r = 0; r < references.length; r++) {
try {
if (references[r].getAttribute(constants.Condition) !== constants.NotNetCoreCondition) { if (references[r].getAttribute(constants.Condition) !== constants.NotNetCoreCondition) {
const filepath = references[r].getAttribute(constants.Include); const filepath = references[r].getAttribute(constants.Include);
if (!filepath) { if (!filepath) {
@@ -190,7 +232,7 @@ export class Project implements ISqlProject {
const name = nameNodes.length === 1 ? nameNodes[0].childNodes[0].nodeValue! : undefined; const name = nameNodes.length === 1 ? nameNodes[0].childNodes[0].nodeValue! : undefined;
const suppressMissingDependenciesErrorNode = references[r].getElementsByTagName(constants.SuppressMissingDependenciesErrors); const suppressMissingDependenciesErrorNode = references[r].getElementsByTagName(constants.SuppressMissingDependenciesErrors);
const suppressMissingDependencies = suppressMissingDependenciesErrorNode[0].childNodes[0].nodeValue === constants.True; const suppressMissingDependencies = suppressMissingDependenciesErrorNode.length === 1 ? (suppressMissingDependenciesErrorNode[0].childNodes[0].nodeValue === constants.True) : false;
const path = utils.convertSlashesForSqlProj(this.getSystemDacpacUri(`${name}.dacpac`).fsPath); const path = utils.convertSlashesForSqlProj(this.getSystemDacpacUri(`${name}.dacpac`).fsPath);
if (path.includes(filepath)) { if (path.includes(filepath)) {
@@ -207,21 +249,32 @@ export class Project implements ISqlProject {
})); }));
} }
} }
} catch (e) {
void window.showErrorMessage(constants.errorReadingProject(constants.DacpacReferenceElement, this.projectFilePath));
console.error(utils.getErrorMessage(e));
}
} }
// find project references // find project references
const projectReferences = this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.ProjectReference); const projectReferences = this.projFileXmlDoc!.documentElement.getElementsByTagName(constants.ProjectReference);
for (let r = 0; r < projectReferences.length; r++) { for (let r = 0; r < projectReferences.length; r++) {
try {
const filepath = projectReferences[r].getAttribute(constants.Include); const filepath = projectReferences[r].getAttribute(constants.Include);
if (!filepath) { if (!filepath) {
throw new Error(constants.invalidDatabaseReference); throw new Error(constants.invalidDatabaseReference);
} }
const nameNodes = projectReferences[r].getElementsByTagName(constants.Name); const nameNodes = projectReferences[r].getElementsByTagName(constants.Name);
const name = nameNodes[0].childNodes[0].nodeValue!; let name = '';
try {
name = nameNodes[0].childNodes[0].nodeValue!;
} catch (e) {
void window.showErrorMessage(constants.errorReadingProject(constants.ProjectReferenceNameElement, this.projectFilePath));
console.error(utils.getErrorMessage(e));
}
const suppressMissingDependenciesErrorNode = projectReferences[r].getElementsByTagName(constants.SuppressMissingDependenciesErrors); const suppressMissingDependenciesErrorNode = projectReferences[r].getElementsByTagName(constants.SuppressMissingDependenciesErrors);
const suppressMissingDependencies = suppressMissingDependenciesErrorNode[0].childNodes[0].nodeValue === constants.True; const suppressMissingDependencies = suppressMissingDependenciesErrorNode.length === 1 ? (suppressMissingDependenciesErrorNode[0].childNodes[0].nodeValue === constants.True) : false;
this._databaseReferences.push(new SqlProjectReferenceProjectEntry({ this._databaseReferences.push(new SqlProjectReferenceProjectEntry({
projectRelativePath: Uri.file(utils.getPlatformSafeFileEntryPath(filepath)), projectRelativePath: Uri.file(utils.getPlatformSafeFileEntryPath(filepath)),
@@ -229,6 +282,10 @@ export class Project implements ISqlProject {
projectGuid: '', // don't care when just reading project as a reference projectGuid: '', // don't care when just reading project as a reference
suppressMissingDependenciesErrors: suppressMissingDependencies suppressMissingDependenciesErrors: suppressMissingDependencies
})); }));
} catch (e) {
void window.showErrorMessage(constants.errorReadingProject(constants.ProjectReferenceElement, this.projectFilePath));
console.error(utils.getErrorMessage(e));
}
} }
} }