Handle connection exceptions and add error callback (#126)

This commit is contained in:
Sharon Ravindran
2016-10-31 11:25:51 -07:00
committed by Karl Burtram
parent a27c182a0f
commit 6937e46c1b
3 changed files with 41 additions and 1 deletions

View File

@@ -21,6 +21,11 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts
/// Summaries of the result sets that were returned with the query
/// </summary>
public BatchSummary[] BatchSummaries { get; set; }
/// <summary>
/// Error message, if any
/// </summary>
public string Message { get; set; }
}
public class QueryExecuteCompleteEvent

View File

@@ -102,6 +102,12 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
/// <param name="q">The query that completed</param>
public delegate Task QueryAsyncEventHandler(Query q);
/// <summary>
/// Delegate type for callback when a query connection fails
/// </summary>
/// <param name="q">The query that completed</param>
public delegate Task QueryAsyncErrorEventHandler(string message);
/// <summary>
/// Callback for when the query has completed successfully
/// </summary>
@@ -112,6 +118,11 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
/// </summary>
public event QueryAsyncEventHandler QueryFailed;
/// <summary>
/// Callback for when the query connection has failed
/// </summary>
public event QueryAsyncErrorEventHandler QueryConnectionException;
/// <summary>
/// The batches underneath this query
/// </summary>
@@ -241,7 +252,19 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
// TODO: Don't create a new connection every time, see TFS #834978
using (DbConnection conn = editorConnection.Factory.CreateSqlConnection(connectionString))
{
await conn.OpenAsync();
try
{
await conn.OpenAsync();
}
catch(Exception exception)
{
this.HasExecuted = true;
if (QueryConnectionException != null)
{
await QueryConnectionException(exception.Message);
}
return;
}
ReliableSqlConnection sqlConn = conn as ReliableSqlConnection;
if (sqlConn != null)

View File

@@ -427,8 +427,20 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
await requestContext.SendEvent(QueryExecuteCompleteEvent.Type, eventParams);
};
Query.QueryAsyncErrorEventHandler errorCallback = async errorMessage =>
{
// Send back the error message
QueryExecuteCompleteParams eventParams = new QueryExecuteCompleteParams
{
OwnerUri = executeParams.OwnerUri,
Message = errorMessage
};
await requestContext.SendEvent(QueryExecuteCompleteEvent.Type, eventParams);
};
query.QueryCompleted += callback;
query.QueryFailed += callback;
query.QueryConnectionException += errorCallback;
// Launch this as an asynchronous task
query.Execute();