mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-03 17:24:53 -05:00
Catch Request/Event handler errors at dispatcher level (#1610)
* Catch Request/Event handler errors at dispatcher level * Fix tests * Use Exception overload of SendError * Fix tests
This commit is contained in:
@@ -63,61 +63,55 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
|
||||
/// Handles schema compare request
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task HandleSchemaCompareRequest(SchemaCompareParams parameters, RequestContext<SchemaCompareResult> requestContext)
|
||||
public Task HandleSchemaCompareRequest(SchemaCompareParams parameters, RequestContext<SchemaCompareResult> requestContext)
|
||||
{
|
||||
try
|
||||
{
|
||||
ConnectionInfo sourceConnInfo;
|
||||
ConnectionInfo targetConnInfo;
|
||||
ConnectionServiceInstance.TryFindConnection(
|
||||
parameters.SourceEndpointInfo.OwnerUri,
|
||||
out sourceConnInfo);
|
||||
ConnectionServiceInstance.TryFindConnection(
|
||||
parameters.TargetEndpointInfo.OwnerUri,
|
||||
out targetConnInfo);
|
||||
ConnectionInfo sourceConnInfo;
|
||||
ConnectionInfo targetConnInfo;
|
||||
ConnectionServiceInstance.TryFindConnection(
|
||||
parameters.SourceEndpointInfo.OwnerUri,
|
||||
out sourceConnInfo);
|
||||
ConnectionServiceInstance.TryFindConnection(
|
||||
parameters.TargetEndpointInfo.OwnerUri,
|
||||
out targetConnInfo);
|
||||
|
||||
CurrentSchemaCompareTask = Task.Run(async () =>
|
||||
CurrentSchemaCompareTask = Task.Run(async () =>
|
||||
{
|
||||
SchemaCompareOperation operation = null;
|
||||
|
||||
try
|
||||
{
|
||||
SchemaCompareOperation operation = null;
|
||||
operation = new SchemaCompareOperation(parameters, sourceConnInfo, targetConnInfo);
|
||||
currentComparisonCancellationAction.Value[operation.OperationId] = operation.Cancel;
|
||||
operation.Execute(parameters.TaskExecutionMode);
|
||||
|
||||
try
|
||||
// add result to dictionary of results
|
||||
schemaCompareResults.Value[operation.OperationId] = operation.ComparisonResult;
|
||||
|
||||
await requestContext.SendResult(new SchemaCompareResult()
|
||||
{
|
||||
operation = new SchemaCompareOperation(parameters, sourceConnInfo, targetConnInfo);
|
||||
currentComparisonCancellationAction.Value[operation.OperationId] = operation.Cancel;
|
||||
operation.Execute(parameters.TaskExecutionMode);
|
||||
OperationId = operation.OperationId,
|
||||
Success = operation.ComparisonResult.IsValid,
|
||||
ErrorMessage = operation.ErrorMessage,
|
||||
AreEqual = operation.ComparisonResult.IsEqual,
|
||||
Differences = operation.Differences
|
||||
});
|
||||
|
||||
// add result to dictionary of results
|
||||
schemaCompareResults.Value[operation.OperationId] = operation.ComparisonResult;
|
||||
|
||||
await requestContext.SendResult(new SchemaCompareResult()
|
||||
{
|
||||
OperationId = operation.OperationId,
|
||||
Success = operation.ComparisonResult.IsValid,
|
||||
ErrorMessage = operation.ErrorMessage,
|
||||
AreEqual = operation.ComparisonResult.IsEqual,
|
||||
Differences = operation.Differences
|
||||
});
|
||||
|
||||
// clean up cancellation action now that the operation is complete (using try remove to avoid exception)
|
||||
Action cancelAction = null;
|
||||
currentComparisonCancellationAction.Value.TryRemove(operation.OperationId, out cancelAction);
|
||||
}
|
||||
catch (Exception e)
|
||||
// clean up cancellation action now that the operation is complete (using try remove to avoid exception)
|
||||
Action cancelAction = null;
|
||||
currentComparisonCancellationAction.Value.TryRemove(operation.OperationId, out cancelAction);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.Write(TraceEventType.Error, "Failed to compare schema. Error: " + e);
|
||||
await requestContext.SendResult(new SchemaCompareResult()
|
||||
{
|
||||
Logger.Write(TraceEventType.Error, "Failed to compare schema. Error: " + e);
|
||||
await requestContext.SendResult(new SchemaCompareResult()
|
||||
{
|
||||
OperationId = operation != null ? operation.OperationId : null,
|
||||
Success = false,
|
||||
ErrorMessage = operation == null ? e.Message : operation.ErrorMessage,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
await requestContext.SendError(e);
|
||||
}
|
||||
OperationId = operation != null ? operation.OperationId : null,
|
||||
Success = false,
|
||||
ErrorMessage = operation == null ? e.Message : operation.ErrorMessage,
|
||||
});
|
||||
}
|
||||
});
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -126,34 +120,26 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
|
||||
/// <returns></returns>
|
||||
public async Task HandleSchemaCompareCancelRequest(SchemaCompareCancelParams parameters, RequestContext<ResultStatus> requestContext)
|
||||
{
|
||||
try
|
||||
Action cancelAction = null;
|
||||
if (currentComparisonCancellationAction.Value.TryRemove(parameters.OperationId, out cancelAction))
|
||||
{
|
||||
Action cancelAction = null;
|
||||
if (currentComparisonCancellationAction.Value.TryRemove(parameters.OperationId, out cancelAction))
|
||||
{
|
||||
if (cancelAction != null)
|
||||
{
|
||||
cancelAction.Invoke();
|
||||
await requestContext.SendResult(new ResultStatus()
|
||||
{
|
||||
Success = true,
|
||||
ErrorMessage = null
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
if (cancelAction != null)
|
||||
{
|
||||
cancelAction.Invoke();
|
||||
await requestContext.SendResult(new ResultStatus()
|
||||
{
|
||||
Success = false,
|
||||
ErrorMessage = SR.SchemaCompareSessionNotFound
|
||||
Success = true,
|
||||
ErrorMessage = null
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
else
|
||||
{
|
||||
await requestContext.SendError(e);
|
||||
await requestContext.SendResult(new ResultStatus()
|
||||
{
|
||||
Success = false,
|
||||
ErrorMessage = SR.SchemaCompareSessionNotFound
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -294,7 +280,7 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
|
||||
operation.Execute(parameters.TaskExecutionMode);
|
||||
|
||||
// update the comparison result if the include/exclude was successful
|
||||
if(operation.Success)
|
||||
if (operation.Success)
|
||||
{
|
||||
schemaCompareResults.Value[parameters.OperationId] = operation.ComparisonResult;
|
||||
}
|
||||
@@ -353,79 +339,66 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
|
||||
/// <returns></returns>
|
||||
public async Task HandleSchemaCompareOpenScmpRequest(SchemaCompareOpenScmpParams parameters, RequestContext<SchemaCompareOpenScmpResult> requestContext)
|
||||
{
|
||||
try
|
||||
CurrentSchemaCompareTask = Task.Run(async () =>
|
||||
{
|
||||
CurrentSchemaCompareTask = Task.Run(async () =>
|
||||
SchemaCompareOpenScmpOperation operation = null;
|
||||
|
||||
try
|
||||
{
|
||||
SchemaCompareOpenScmpOperation operation = null;
|
||||
operation = new SchemaCompareOpenScmpOperation(parameters);
|
||||
operation.Execute(TaskExecutionMode.Execute);
|
||||
|
||||
try
|
||||
await requestContext.SendResult(operation.Result);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
await requestContext.SendResult(new SchemaCompareOpenScmpResult()
|
||||
{
|
||||
operation = new SchemaCompareOpenScmpOperation(parameters);
|
||||
operation.Execute(TaskExecutionMode.Execute);
|
||||
|
||||
await requestContext.SendResult(operation.Result);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
await requestContext.SendResult(new SchemaCompareOpenScmpResult()
|
||||
{
|
||||
Success = false,
|
||||
ErrorMessage = operation == null ? e.Message : operation.ErrorMessage,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
await requestContext.SendError(e);
|
||||
}
|
||||
Success = false,
|
||||
ErrorMessage = operation == null ? e.Message : operation.ErrorMessage,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles schema compare save SCMP request
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task HandleSchemaCompareSaveScmpRequest(SchemaCompareSaveScmpParams parameters, RequestContext<ResultStatus> requestContext)
|
||||
public Task HandleSchemaCompareSaveScmpRequest(SchemaCompareSaveScmpParams parameters, RequestContext<ResultStatus> requestContext)
|
||||
{
|
||||
try
|
||||
{
|
||||
ConnectionInfo sourceConnInfo;
|
||||
ConnectionInfo targetConnInfo;
|
||||
ConnectionServiceInstance.TryFindConnection(parameters.SourceEndpointInfo.OwnerUri, out sourceConnInfo);
|
||||
ConnectionServiceInstance.TryFindConnection(parameters.TargetEndpointInfo.OwnerUri, out targetConnInfo);
|
||||
ConnectionInfo sourceConnInfo;
|
||||
ConnectionInfo targetConnInfo;
|
||||
ConnectionServiceInstance.TryFindConnection(parameters.SourceEndpointInfo.OwnerUri, out sourceConnInfo);
|
||||
ConnectionServiceInstance.TryFindConnection(parameters.TargetEndpointInfo.OwnerUri, out targetConnInfo);
|
||||
|
||||
CurrentSchemaCompareTask = Task.Run(async () =>
|
||||
CurrentSchemaCompareTask = Task.Run(async () =>
|
||||
{
|
||||
SchemaCompareSaveScmpOperation operation = null;
|
||||
|
||||
try
|
||||
{
|
||||
SchemaCompareSaveScmpOperation operation = null;
|
||||
operation = new SchemaCompareSaveScmpOperation(parameters, sourceConnInfo, targetConnInfo);
|
||||
operation.Execute(parameters.TaskExecutionMode);
|
||||
|
||||
try
|
||||
await requestContext.SendResult(new ResultStatus()
|
||||
{
|
||||
operation = new SchemaCompareSaveScmpOperation(parameters, sourceConnInfo, targetConnInfo);
|
||||
operation.Execute(parameters.TaskExecutionMode);
|
||||
|
||||
await requestContext.SendResult(new ResultStatus()
|
||||
{
|
||||
Success = true,
|
||||
ErrorMessage = operation.ErrorMessage,
|
||||
});
|
||||
}
|
||||
catch (Exception e)
|
||||
Success = true,
|
||||
ErrorMessage = operation.ErrorMessage,
|
||||
});
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.Write(TraceEventType.Error, "Failed to save scmp file. Error: " + e);
|
||||
await requestContext.SendResult(new SchemaCompareResult()
|
||||
{
|
||||
Logger.Write(TraceEventType.Error, "Failed to save scmp file. Error: " + e);
|
||||
await requestContext.SendResult(new SchemaCompareResult()
|
||||
{
|
||||
OperationId = operation != null ? operation.OperationId : null,
|
||||
Success = false,
|
||||
ErrorMessage = operation == null ? e.Message : operation.ErrorMessage,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
await requestContext.SendError(e);
|
||||
}
|
||||
OperationId = operation != null ? operation.OperationId : null,
|
||||
Success = false,
|
||||
ErrorMessage = operation == null ? e.Message : operation.ErrorMessage,
|
||||
});
|
||||
}
|
||||
});
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private SqlTaskManager SqlTaskManagerInstance
|
||||
|
||||
Reference in New Issue
Block a user