diff --git a/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Contracts/QueryExecuteCompleteNotification.cs b/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Contracts/QueryExecuteCompleteNotification.cs index 90c8c7b3..8375235a 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Contracts/QueryExecuteCompleteNotification.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Contracts/QueryExecuteCompleteNotification.cs @@ -21,6 +21,11 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts /// Summaries of the result sets that were returned with the query /// public BatchSummary[] BatchSummaries { get; set; } + + /// + /// Error message, if any + /// + public string Message { get; set; } } public class QueryExecuteCompleteEvent diff --git a/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Query.cs b/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Query.cs index 61b17543..d47fafca 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Query.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/Query.cs @@ -102,6 +102,12 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution /// The query that completed public delegate Task QueryAsyncEventHandler(Query q); + /// + /// Delegate type for callback when a query connection fails + /// + /// The query that completed + public delegate Task QueryAsyncErrorEventHandler(string message); + /// /// Callback for when the query has completed successfully /// @@ -112,6 +118,11 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution /// public event QueryAsyncEventHandler QueryFailed; + /// + /// Callback for when the query connection has failed + /// + public event QueryAsyncErrorEventHandler QueryConnectionException; + /// /// The batches underneath this query /// @@ -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) diff --git a/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/QueryExecutionService.cs b/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/QueryExecutionService.cs index ea69514b..c7614596 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/QueryExecutionService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/QueryExecution/QueryExecutionService.cs @@ -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();