show error when scripting fails (#20843)

This commit is contained in:
Alan Ren
2022-10-13 16:55:40 -07:00
committed by GitHub
parent 38eb0c8429
commit 23dfe62742
3 changed files with 98 additions and 86 deletions

View File

@@ -21,7 +21,8 @@ export class ScriptSelectAction extends Action {
id: string, label: string, id: string, label: string,
@IQueryEditorService protected _queryEditorService: IQueryEditorService, @IQueryEditorService protected _queryEditorService: IQueryEditorService,
@IConnectionManagementService protected _connectionManagementService: IConnectionManagementService, @IConnectionManagementService protected _connectionManagementService: IConnectionManagementService,
@IScriptingService protected _scriptingService: IScriptingService @IScriptingService protected _scriptingService: IScriptingService,
@IErrorMessageService protected _errorMessageService: IErrorMessageService
) { ) {
super(id, label); super(id, label);
} }
@@ -32,7 +33,8 @@ export class ScriptSelectAction extends Action {
actionContext.object!, actionContext.object!,
this._connectionManagementService, this._connectionManagementService,
this._queryEditorService, this._queryEditorService,
this._scriptingService this._scriptingService,
this._errorMessageService
); );
} }
} }
@@ -97,9 +99,10 @@ export class EditDataAction extends Action {
constructor( constructor(
id: string, label: string, id: string, label: string,
@IQueryEditorService protected _queryEditorService: IQueryEditorService, @IQueryEditorService private _queryEditorService: IQueryEditorService,
@IConnectionManagementService protected _connectionManagementService: IConnectionManagementService, @IConnectionManagementService private _connectionManagementService: IConnectionManagementService,
@IScriptingService protected _scriptingService: IScriptingService @IScriptingService private _scriptingService: IScriptingService,
@IErrorMessageService private _errorMessageService: IErrorMessageService
) { ) {
super(id, label); super(id, label);
} }
@@ -110,7 +113,8 @@ export class EditDataAction extends Action {
actionContext.object!, actionContext.object!,
this._connectionManagementService, this._connectionManagementService,
this._queryEditorService, this._queryEditorService,
this._scriptingService this._scriptingService,
this._errorMessageService
); );
} }
} }

View File

@@ -37,10 +37,14 @@ const targetDatabaseEngineEditionMap = {
11: 'SqlServerOnDemandEdition', 11: 'SqlServerOnDemandEdition',
}; };
const ScriptingFailedDialogTitle = nls.localize('scriptingFailed', "Scripting Failed");
const SelectScriptNotGeneratedError = nls.localize('selectScriptNotGeneratedError', "Failed to generate select script for the selected object.");
/** /**
* Select the top rows from an object * Select the top rows from an object
*/ */
export async function scriptSelect(connectionProfile: IConnectionProfile, metadata: azdata.ObjectMetadata, connectionService: IConnectionManagementService, queryEditorService: IQueryEditorService, scriptingService: IScriptingService): Promise<boolean> { export async function scriptSelect(connectionProfile: IConnectionProfile, metadata: azdata.ObjectMetadata, connectionService: IConnectionManagementService, queryEditorService: IQueryEditorService, scriptingService: IScriptingService, errorMessageService: IErrorMessageService): Promise<void> {
try {
const connectionResult = await connectionService.connectIfNotConnected(connectionProfile); const connectionResult = await connectionService.connectIfNotConnected(connectionProfile);
let paramDetails = getScriptingParamDetails(connectionService, connectionResult, metadata)!; let paramDetails = getScriptingParamDetails(connectionService, connectionResult, metadata)!;
const result = await scriptingService.script(connectionResult, metadata, ScriptOperation.Select, paramDetails); const result = await scriptingService.script(connectionResult, metadata, ScriptOperation.Select, paramDetails);
@@ -54,19 +58,20 @@ export async function scriptSelect(connectionProfile: IConnectionProfile, metada
showConnectionDialogOnError: true, showConnectionDialogOnError: true,
showFirewallRuleOnError: true showFirewallRuleOnError: true
}; };
const innerConnectionResult = await connectionService.connect(connectionProfile, owner.uri, options); await connectionService.connect(connectionProfile, owner.uri, options);
return Boolean(innerConnectionResult) && innerConnectionResult.connected;
} else { } else {
let errMsg: string = nls.localize('scriptSelectNotFound', "No script was returned when calling select script on object "); throw new Error(SelectScriptNotGeneratedError);
throw new Error(errMsg.concat(metadata.metadataTypeName)); }
} catch (err) {
errorMessageService.showDialog(Severity.Error, ScriptingFailedDialogTitle, err?.message ?? err);
} }
} }
/** /**
* Opens a new Edit Data session * Opens a new Edit Data session
*/ */
export async function scriptEditSelect(connectionProfile: IConnectionProfile, metadata: azdata.ObjectMetadata, connectionService: IConnectionManagementService, queryEditorService: IQueryEditorService, scriptingService: IScriptingService): Promise<boolean> { export async function scriptEditSelect(connectionProfile: IConnectionProfile, metadata: azdata.ObjectMetadata, connectionService: IConnectionManagementService, queryEditorService: IQueryEditorService, scriptingService: IScriptingService, errorMessageService: IErrorMessageService): Promise<void> {
try {
const connectionResult = await connectionService.connectIfNotConnected(connectionProfile); const connectionResult = await connectionService.connectIfNotConnected(connectionProfile);
let paramDetails = getScriptingParamDetails(connectionService, connectionResult, metadata); let paramDetails = getScriptingParamDetails(connectionService, connectionResult, metadata);
const result = await scriptingService.script(connectionResult, metadata, ScriptOperation.Select, paramDetails!); const result = await scriptingService.script(connectionResult, metadata, ScriptOperation.Select, paramDetails!);
@@ -80,12 +85,13 @@ export async function scriptEditSelect(connectionProfile: IConnectionProfile, me
showConnectionDialogOnError: true, showConnectionDialogOnError: true,
showFirewallRuleOnError: true showFirewallRuleOnError: true
}; };
const innerConnectionResult = await connectionService.connect(connectionProfile, owner.uri, options); await connectionService.connect(connectionProfile, owner.uri, options);
return Boolean(innerConnectionResult) && innerConnectionResult.connected;
} else { } else {
let errMsg: string = nls.localize('scriptSelectNotFound', "No script was returned when calling select script on object "); throw new Error(SelectScriptNotGeneratedError);
throw new Error(errMsg.concat(metadata.metadataTypeName)); }
}
catch (err) {
errorMessageService.showDialog(Severity.Error, ScriptingFailedDialogTitle, err?.message ?? err);
} }
} }
@@ -118,7 +124,8 @@ export async function script(connectionProfile: IConnectionProfile, metadata: az
queryEditorService: IQueryEditorService, queryEditorService: IQueryEditorService,
scriptingService: IScriptingService, scriptingService: IScriptingService,
operation: ScriptOperation, operation: ScriptOperation,
errorMessageService: IErrorMessageService): Promise<boolean> { errorMessageService: IErrorMessageService): Promise<void> {
try {
const connectionResult = await connectionService.connectIfNotConnected(connectionProfile); const connectionResult = await connectionService.connectIfNotConnected(connectionProfile);
let paramDetails = getScriptingParamDetails(connectionService, connectionResult, metadata)!; let paramDetails = getScriptingParamDetails(connectionService, connectionResult, metadata)!;
const result = await scriptingService.script(connectionResult, metadata, operation, paramDetails); const result = await scriptingService.script(connectionResult, metadata, operation, paramDetails);
@@ -136,10 +143,7 @@ export async function script(connectionProfile: IConnectionProfile, metadata: az
showConnectionDialogOnError: true, showConnectionDialogOnError: true,
showFirewallRuleOnError: true showFirewallRuleOnError: true
}; };
const innerConnectionResult = await connectionService.connect(connectionProfile, owner.uri, options); await connectionService.connect(connectionProfile, owner.uri, options);
return Boolean(innerConnectionResult) && innerConnectionResult.connected;
} else { } else {
let scriptNotFoundMsg = nls.localize('scriptNotFoundForObject', "No script was returned when scripting as {0} on object {1}", let scriptNotFoundMsg = nls.localize('scriptNotFoundForObject', "No script was returned when scripting as {0} on object {1}",
GetScriptOperationName(operation), metadata.metadataTypeName); GetScriptOperationName(operation), metadata.metadataTypeName);
@@ -150,14 +154,15 @@ export async function script(connectionProfile: IConnectionProfile, metadata: az
messageDetail = operationResult.errorDetails; messageDetail = operationResult.errorDetails;
} }
if (errorMessageService) { if (errorMessageService) {
let title = nls.localize('scriptingFailed', "Scripting Failed"); errorMessageService.showDialog(Severity.Error, ScriptingFailedDialogTitle, scriptNotFoundMsg, messageDetail);
errorMessageService.showDialog(Severity.Error, title, scriptNotFoundMsg, messageDetail);
} }
throw new Error(scriptNotFoundMsg);
} }
} else { } else {
throw new Error(nls.localize('scriptNotFound', "No script was returned when scripting as {0}", GetScriptOperationName(operation))); throw new Error(nls.localize('scriptNotFound', "No script was returned when scripting as {0}", GetScriptOperationName(operation)));
} }
} catch (err) {
errorMessageService.showDialog(Severity.Error, ScriptingFailedDialogTitle, err?.message ?? err);
}
} }
function getScriptingParamDetails(connectionService: IConnectionManagementService, ownerUri: string, metadata: azdata.ObjectMetadata): azdata.ScriptingParamDetails | undefined { function getScriptingParamDetails(connectionService: IConnectionManagementService, ownerUri: string, metadata: azdata.ObjectMetadata): azdata.ScriptingParamDetails | undefined {

View File

@@ -96,13 +96,14 @@ CommandsRegistry.registerCommand({
const connectionManagementService = accessor.get(IConnectionManagementService); const connectionManagementService = accessor.get(IConnectionManagementService);
const scriptingService = accessor.get(IScriptingService); const scriptingService = accessor.get(IScriptingService);
const progressService = accessor.get(IProgressService); const progressService = accessor.get(IProgressService);
const errorMessageService = accessor.get(IErrorMessageService);
const profile = new ConnectionProfile(capabilitiesService, args.$treeItem.payload); const profile = new ConnectionProfile(capabilitiesService, args.$treeItem.payload);
const baseContext: BaseActionContext = { const baseContext: BaseActionContext = {
profile: profile, profile: profile,
object: oeShimService.getNodeInfoForTreeItem(args.$treeItem)!.metadata object: oeShimService.getNodeInfoForTreeItem(args.$treeItem)!.metadata
}; };
const scriptSelectAction = new ScriptSelectAction(ScriptSelectAction.ID, ScriptSelectAction.LABEL, const scriptSelectAction = new ScriptSelectAction(ScriptSelectAction.ID, ScriptSelectAction.LABEL,
queryEditorService, connectionManagementService, scriptingService); queryEditorService, connectionManagementService, scriptingService, errorMessageService);
await progressService.withProgress({ location: VIEWLET_ID }, async () => await scriptSelectAction.run(baseContext)); await progressService.withProgress({ location: VIEWLET_ID }, async () => await scriptSelectAction.run(baseContext));
} }
} }
@@ -167,13 +168,14 @@ CommandsRegistry.registerCommand({
const connectionManagementService = accessor.get(IConnectionManagementService); const connectionManagementService = accessor.get(IConnectionManagementService);
const scriptingService = accessor.get(IScriptingService); const scriptingService = accessor.get(IScriptingService);
const progressService = accessor.get(IProgressService); const progressService = accessor.get(IProgressService);
const errorMessageService = accessor.get(IErrorMessageService);
const profile = new ConnectionProfile(capabilitiesService, args.$treeItem.payload); const profile = new ConnectionProfile(capabilitiesService, args.$treeItem.payload);
const baseContext: BaseActionContext = { const baseContext: BaseActionContext = {
profile: profile, profile: profile,
object: oeShimService.getNodeInfoForTreeItem(args.$treeItem)!.metadata object: oeShimService.getNodeInfoForTreeItem(args.$treeItem)!.metadata
}; };
const editDataAction = new EditDataAction(EditDataAction.ID, EditDataAction.LABEL, const editDataAction = new EditDataAction(EditDataAction.ID, EditDataAction.LABEL,
queryEditorService, connectionManagementService, scriptingService); queryEditorService, connectionManagementService, scriptingService, errorMessageService);
await progressService.withProgress({ location: VIEWLET_ID }, async () => await editDataAction.run(baseContext)); await progressService.withProgress({ location: VIEWLET_ID }, async () => await editDataAction.run(baseContext));
} }
} }
@@ -356,9 +358,10 @@ export class ExplorerScriptSelectAction extends ScriptSelectAction {
@IQueryEditorService queryEditorService: IQueryEditorService, @IQueryEditorService queryEditorService: IQueryEditorService,
@IConnectionManagementService connectionManagementService: IConnectionManagementService, @IConnectionManagementService connectionManagementService: IConnectionManagementService,
@IScriptingService scriptingService: IScriptingService, @IScriptingService scriptingService: IScriptingService,
@IProgressService private readonly progressService: IProgressService @IProgressService private readonly progressService: IProgressService,
@IErrorMessageService errorMessageService: IErrorMessageService
) { ) {
super(id, label, queryEditorService, connectionManagementService, scriptingService); super(id, label, queryEditorService, connectionManagementService, scriptingService, errorMessageService);
} }
public override async run(actionContext: BaseActionContext): Promise<void> { public override async run(actionContext: BaseActionContext): Promise<void> {