mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 01:25:40 -05:00
Handle connection exceptions and add error callback (#126)
This commit is contained in:
committed by
Karl Burtram
parent
a27c182a0f
commit
6937e46c1b
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user