diff --git a/src/Microsoft.SqlTools.Hosting/Hosting/Protocol/MessageDispatcher.cs b/src/Microsoft.SqlTools.Hosting/Hosting/Protocol/MessageDispatcher.cs index 36661b9a..3ef41dc9 100644 --- a/src/Microsoft.SqlTools.Hosting/Hosting/Protocol/MessageDispatcher.cs +++ b/src/Microsoft.SqlTools.Hosting/Hosting/Protocol/MessageDispatcher.cs @@ -158,29 +158,12 @@ namespace Microsoft.SqlTools.Hosting.Protocol } catch (Exception ex) { - Logger.Error($"{requestType.MethodName} : {GetErrorMessage(ex, true)}"); - await requestContext.SendError(GetErrorMessage(ex)); + Logger.Error($"{requestType.MethodName} : {ex.GetFullErrorMessage(true)}"); + await requestContext.SendError(ex.GetFullErrorMessage()); } }); } - private string GetErrorMessage(Exception e, bool includeStackTrace = false) - { - List errors = new List(); - - while (e != null) - { - errors.Add(e.Message); - if (includeStackTrace) - { - errors.Add(e.StackTrace); - } - e = e.InnerException; - } - - return errors.Count > 0 ? string.Join(includeStackTrace ? Environment.NewLine : " ---> ", errors) : string.Empty; - } - public void SetEventHandler( EventType eventType, Func eventHandler) diff --git a/src/Microsoft.SqlTools.Hosting/Utility/Extensions.cs b/src/Microsoft.SqlTools.Hosting/Utility/Extensions.cs index 142bd9cf..70daac58 100644 --- a/src/Microsoft.SqlTools.Hosting/Utility/Extensions.cs +++ b/src/Microsoft.SqlTools.Hosting/Utility/Extensions.cs @@ -4,6 +4,7 @@ // using System; +using System.Collections.Generic; namespace Microsoft.SqlTools.Utility { @@ -77,5 +78,22 @@ namespace Microsoft.SqlTools.Utility return false; } + + public static string GetFullErrorMessage(this Exception e, bool includeStackTrace = false) + { + List errors = new List(); + + while (e != null) + { + errors.Add(e.Message); + if (includeStackTrace) + { + errors.Add(e.StackTrace); + } + e = e.InnerException; + } + + return errors.Count > 0 ? string.Join(includeStackTrace ? Environment.NewLine : " ---> ", errors) : string.Empty; + } } } diff --git a/src/Microsoft.SqlTools.ServiceLayer/Agent/AgentService.cs b/src/Microsoft.SqlTools.ServiceLayer/Agent/AgentService.cs index 05bee606..3c65a183 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Agent/AgentService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Agent/AgentService.cs @@ -20,6 +20,7 @@ using Microsoft.SqlTools.ServiceLayer.Connection; using Microsoft.SqlTools.ServiceLayer.Hosting; using Microsoft.SqlTools.ServiceLayer.Management; using Microsoft.SqlTools.ServiceLayer.Utility; +using Microsoft.SqlTools.Utility; namespace Microsoft.SqlTools.ServiceLayer.Agent { @@ -153,25 +154,40 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent parameters.OwnerUri, out connInfo); - if (connInfo != null) + try { - var serverConnection = ConnectionService.OpenServerConnection(connInfo); - var fetcher = new JobFetcher(serverConnection); - var filter = new JobActivityFilter(); - var jobs = fetcher.FetchJobs(filter); - var agentJobs = new List(); - if (jobs != null) + if (connInfo != null) { - foreach (var job in jobs.Values) + var serverConnection = ConnectionService.OpenServerConnection(connInfo); + var fetcher = new JobFetcher(serverConnection); + var filter = new JobActivityFilter(); + var jobs = fetcher.FetchJobs(filter); + var agentJobs = new List(); + if (jobs != null) { - agentJobs.Add(AgentUtilities.ConvertToAgentJobInfo(job)); + foreach (var job in jobs.Values) + { + agentJobs.Add(AgentUtilities.ConvertToAgentJobInfo(job)); + } } + result.Success = true; + result.Jobs = agentJobs.ToArray(); + serverConnection.SqlConnectionObject.Close(); } - result.Success = true; - result.Jobs = agentJobs.ToArray(); - serverConnection.SqlConnectionObject.Close(); + await requestContext.SendResult(result); + } + catch (Exception ex) + { + result.Success = false; + result.ErrorMessage = ex.Message; + Exception exception = ex.InnerException; + while (exception != null) + { + result.ErrorMessage += Environment.NewLine + "\t" + exception.Message; + exception = exception.InnerException; + } + await requestContext.SendResult(result); } - await requestContext.SendResult(result); } /// @@ -180,69 +196,84 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent internal async Task HandleJobHistoryRequest(AgentJobHistoryParams parameters, RequestContext requestContext) { var result = new AgentJobHistoryResult(); - ConnectionInfo connInfo; - ConnectionServiceInstance.TryFindConnection( - parameters.OwnerUri, - out connInfo); - if (connInfo != null) + try { - CDataContainer dataContainer = CDataContainer.CreateDataContainer(connInfo, databaseExists: true); - var jobServer = dataContainer.Server.JobServer; - var jobs = jobServer.Jobs; - Tuple tuple = CreateSqlConnection(connInfo, parameters.JobId); - SqlConnectionInfo sqlConnInfo = tuple.Item1; - DataTable dt = tuple.Item2; - ServerConnection connection = tuple.Item3; - - // Send Steps, Alerts and Schedules with job history in background - // Add steps to the job if any - JobStepCollection steps = jobs[parameters.JobName].JobSteps; - var jobSteps = new List(); - foreach (JobStep step in steps) + ConnectionInfo connInfo; + ConnectionServiceInstance.TryFindConnection( + parameters.OwnerUri, + out connInfo); + if (connInfo != null) { - jobSteps.Add(AgentUtilities.ConvertToAgentJobStepInfo(step, parameters.JobId, parameters.JobName)); - } - result.Steps = jobSteps.ToArray(); + CDataContainer dataContainer = CDataContainer.CreateDataContainer(connInfo, databaseExists: true); + var jobServer = dataContainer.Server.JobServer; + var jobs = jobServer.Jobs; + Tuple tuple = CreateSqlConnection(connInfo, parameters.JobId); + SqlConnectionInfo sqlConnInfo = tuple.Item1; + DataTable dt = tuple.Item2; + ServerConnection connection = tuple.Item3; - // Add schedules to the job if any - JobScheduleCollection schedules = jobs[parameters.JobName].JobSchedules; - var jobSchedules = new List(); - foreach (JobSchedule schedule in schedules) - { - jobSchedules.Add(AgentUtilities.ConvertToAgentScheduleInfo(schedule)); - } - result.Schedules = jobSchedules.ToArray(); - - // Alerts - AlertCollection alerts = jobServer.Alerts; - var jobAlerts = new List(); - foreach (Alert alert in alerts) - { - if (alert.JobName == parameters.JobName) + // Send Steps, Alerts and Schedules with job history in background + // Add steps to the job if any + JobStepCollection steps = jobs[parameters.JobName].JobSteps; + var jobSteps = new List(); + foreach (JobStep step in steps) { - jobAlerts.Add(alert); + jobSteps.Add(AgentUtilities.ConvertToAgentJobStepInfo(step, parameters.JobId, parameters.JobName)); } + result.Steps = jobSteps.ToArray(); + + // Add schedules to the job if any + JobScheduleCollection schedules = jobs[parameters.JobName].JobSchedules; + var jobSchedules = new List(); + foreach (JobSchedule schedule in schedules) + { + jobSchedules.Add(AgentUtilities.ConvertToAgentScheduleInfo(schedule)); + } + result.Schedules = jobSchedules.ToArray(); + + // Alerts + AlertCollection alerts = jobServer.Alerts; + var jobAlerts = new List(); + foreach (Alert alert in alerts) + { + if (alert.JobName == parameters.JobName) + { + jobAlerts.Add(alert); + } + } + result.Alerts = AgentUtilities.ConvertToAgentAlertInfo(jobAlerts); + + // Add histories + int count = dt.Rows.Count; + List jobHistories = new List(); + if (count > 0) + { + var job = dt.Rows[0]; + Guid jobId = (Guid)job[AgentUtilities.UrnJobId]; + int runStatus = Convert.ToInt32(job[AgentUtilities.UrnRunStatus], System.Globalization.CultureInfo.InvariantCulture); + var t = new LogSourceJobHistory(parameters.JobName, sqlConnInfo, null, runStatus, jobId, null); + var tlog = t as ILogSource; + tlog.Initialize(); + var logEntries = t.LogEntries; + + // Finally add the job histories + jobHistories = AgentUtilities.ConvertToAgentJobHistoryInfo(logEntries, job, steps); + result.Histories = jobHistories.ToArray(); + result.Success = true; + tlog.CloseReader(); + } + await requestContext.SendResult(result); } - result.Alerts = AgentUtilities.ConvertToAgentAlertInfo(jobAlerts); - - // Add histories - int count = dt.Rows.Count; - List jobHistories = new List(); - if (count > 0) + } + catch (Exception ex) + { + result.Success = false; + result.ErrorMessage = ex.Message; + Exception exception = ex.InnerException; + while (exception != null) { - var job = dt.Rows[0]; - Guid jobId = (Guid)job[AgentUtilities.UrnJobId]; - int runStatus = Convert.ToInt32(job[AgentUtilities.UrnRunStatus], System.Globalization.CultureInfo.InvariantCulture); - var t = new LogSourceJobHistory(parameters.JobName, sqlConnInfo, null, runStatus, jobId, null); - var tlog = t as ILogSource; - tlog.Initialize(); - var logEntries = t.LogEntries; - - // Finally add the job histories - jobHistories = AgentUtilities.ConvertToAgentJobHistoryInfo(logEntries, job, steps); - result.Histories = jobHistories.ToArray(); - result.Success = true; - tlog.CloseReader(); + result.ErrorMessage += Environment.NewLine + "\t" + exception.Message; + exception = exception.InnerException; } await requestContext.SendResult(result); } @@ -1201,7 +1232,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent catch (Exception e) { result.Success = false; - result.ErrorMessage = e.ToString(); + result.ErrorMessage = e.GetFullErrorMessage(); } await requestContext.SendResult(result); } @@ -1230,7 +1261,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent catch (Exception e) { result.Success = false; - result.ErrorMessage = e.ToString(); + result.ErrorMessage = e.GetFullErrorMessage(); } await requestContext.SendResult(result); } @@ -1251,7 +1282,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent catch (Exception e) { result.Success = false; - result.ErrorMessage = e.ToString(); + result.ErrorMessage = e.GetFullErrorMessage(); } await requestContext.SendResult(result); @@ -1273,7 +1304,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent catch (Exception e) { result.Success = false; - result.ErrorMessage = e.ToString(); + result.ErrorMessage = e.GetFullErrorMessage(); } await requestContext.SendResult(result); @@ -1298,7 +1329,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent catch (Exception e) { result.Success = false; - result.ErrorMessage = e.ToString(); + result.ErrorMessage = e.GetFullErrorMessage(); } await requestContext.SendResult(result); } @@ -1320,7 +1351,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent catch (Exception e) { result.Success = false; - result.ErrorMessage = e.ToString(); + result.ErrorMessage = e.GetFullErrorMessage(); } await requestContext.SendResult(result); } @@ -1343,8 +1374,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent catch (Exception e) { result.Success = false; - result.ErrorMessage = e.ToString(); - + result.ErrorMessage = e.GetFullErrorMessage(); } await requestContext.SendResult(result); } @@ -1369,8 +1399,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent catch (Exception e) { result.Success = false; - result.ErrorMessage = e.ToString(); - + result.ErrorMessage = e.GetFullErrorMessage(); } await requestContext.SendResult(result); } @@ -1395,8 +1424,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent catch (Exception e) { result.Success = false; - result.ErrorMessage = e.ToString(); - + result.ErrorMessage = e.GetFullErrorMessage(); } await requestContext.SendResult(result); } @@ -1420,8 +1448,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent catch (Exception e) { result.Success = false; - result.ErrorMessage = e.ToString(); - + result.ErrorMessage = e.GetFullErrorMessage(); } await requestContext.SendResult(result); } diff --git a/src/Microsoft.SqlTools.ServiceLayer/Connection/ConnectionService.cs b/src/Microsoft.SqlTools.ServiceLayer/Connection/ConnectionService.cs index 134bd06f..26a63263 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Connection/ConnectionService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Connection/ConnectionService.cs @@ -1878,6 +1878,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection /// The connection info to connect with /// A plaintext string that will be included in the application name for the connection /// A SqlConnection created with the given connection info + /// When an error occurs. public static SqlConnection OpenSqlConnection(ConnectionInfo connInfo, string featureName = null) { try @@ -1927,9 +1928,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection "Failed opening a SqlConnection: error:{0} inner:{1} stacktrace:{2}", ex.Message, ex.InnerException != null ? ex.InnerException.Message : string.Empty, ex.StackTrace); Logger.Error(error); + throw; } - - return null; } /// diff --git a/src/Microsoft.SqlTools.ServiceLayer/LanguageServices/ConnectedBindingQueue.cs b/src/Microsoft.SqlTools.ServiceLayer/LanguageServices/ConnectedBindingQueue.cs index aa23fe4f..1b605e43 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/LanguageServices/ConnectedBindingQueue.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/LanguageServices/ConnectedBindingQueue.cs @@ -124,9 +124,9 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices && entry.Key != "groupId" && entry.Key != "password" && entry.Key != "connectionName") { // Boolean values are explicitly labeled true or false instead of undefined. - if (entry.Value is bool) + if (entry.Value is bool v) { - if ((bool)entry.Value) + if (v) { key += "_" + entry.Key + ":true"; } diff --git a/src/Microsoft.SqlTools.ServiceLayer/ModelManagement/ModelManagementService.cs b/src/Microsoft.SqlTools.ServiceLayer/ModelManagement/ModelManagementService.cs index 6eefb793..abd142fa 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/ModelManagement/ModelManagementService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/ModelManagement/ModelManagementService.cs @@ -80,9 +80,9 @@ namespace Microsoft.SqlTools.ServiceLayer.ModelManagement public async Task HandleModelImportRequest(ImportModelRequestParams parameters, RequestContext requestContext) { Logger.Verbose("HandleModelImportRequest"); - ImportModelResponseParams response = new ImportModelResponseParams - { - }; + ImportModelResponseParams response = new ImportModelResponseParams + { + }; await HandleRequest(parameters, response, requestContext, (dbConnection, parameters, response) => { @@ -205,8 +205,8 @@ namespace Microsoft.SqlTools.ServiceLayer.ModelManagement } private async Task HandleRequest( - T parameters, - TResponse response, + T parameters, + TResponse response, RequestContext requestContext, Func operation) where T : ModelRequestBase where TResponse : ModelResponseBase { @@ -232,6 +232,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ModelManagement catch (Exception e) { // Exception related to run task will be captured here + Logger.Error(e); await requestContext.SendError(e); } } diff --git a/src/Microsoft.SqlTools.ServiceLayer/ServerConfigurations/ServerConfigService.cs b/src/Microsoft.SqlTools.ServiceLayer/ServerConfigurations/ServerConfigService.cs index ac5b3a9b..d30740cf 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/ServerConfigurations/ServerConfigService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/ServerConfigurations/ServerConfigService.cs @@ -66,30 +66,22 @@ namespace Microsoft.SqlTools.ServiceLayer.ServerConfigurations public async Task HandleServerConfigViewRequest(ServerConfigViewRequestParams parameters, RequestContext requestContext) { Logger.Verbose("HandleServerConfigViewRequest"); - try + ConnectionInfo connInfo; + ConnectionServiceInstance.TryFindConnection( + parameters.OwnerUri, + out connInfo); + if (connInfo == null) { - ConnectionInfo connInfo; - ConnectionServiceInstance.TryFindConnection( - parameters.OwnerUri, - out connInfo); - if (connInfo == null) - { - await requestContext.SendError(new Exception(SR.ProfilerConnectionNotFound)); - } - else - { - var serverConnection = ConnectionService.OpenServerConnection(connInfo); - ServerConfigProperty serverConfig = GetConfig(serverConnection, parameters.ConfigNumber); - await requestContext.SendResult(new ServerConfigViewResponseParams - { - ConfigProperty = serverConfig - }); - } + await requestContext.SendError(new Exception(SR.ProfilerConnectionNotFound)); } - catch (Exception e) + else { - // Exception related to run task will be captured here - await requestContext.SendError(e); + var serverConnection = ConnectionService.OpenServerConnection(connInfo); + ServerConfigProperty serverConfig = GetConfig(serverConnection, parameters.ConfigNumber); + await requestContext.SendResult(new ServerConfigViewResponseParams + { + ConfigProperty = serverConfig + }); } } @@ -102,32 +94,24 @@ namespace Microsoft.SqlTools.ServiceLayer.ServerConfigurations public async Task HandleServerConfigUpdateRequest(ServerConfigUpdateRequestParams parameters, RequestContext requestContext) { Logger.Verbose("HandleServerConfigUpdateRequest"); - try + ConnectionInfo connInfo; + ConnectionServiceInstance.TryFindConnection( + parameters.OwnerUri, + out connInfo); + ServerConfigUpdateResponseParams response = new ServerConfigUpdateResponseParams { - ConnectionInfo connInfo; - ConnectionServiceInstance.TryFindConnection( - parameters.OwnerUri, - out connInfo); - ServerConfigUpdateResponseParams response = new ServerConfigUpdateResponseParams - { - }; + }; - if (connInfo == null) - { - await requestContext.SendError(new Exception(SR.ProfilerConnectionNotFound)); - } - else - { - var serverConnection = ConnectionService.OpenServerConnection(connInfo); - UpdateConfig(serverConnection, parameters.ConfigNumber, parameters.ConfigValue); - response.ConfigProperty = GetConfig(serverConnection, parameters.ConfigNumber); - await requestContext.SendResult(response); - } - } - catch (Exception e) + if (connInfo == null) { - // Exception related to run task will be captured here - await requestContext.SendError(e); + await requestContext.SendError(new Exception(SR.ProfilerConnectionNotFound)); + } + else + { + var serverConnection = ConnectionService.OpenServerConnection(connInfo); + UpdateConfig(serverConnection, parameters.ConfigNumber, parameters.ConfigValue); + response.ConfigProperty = GetConfig(serverConnection, parameters.ConfigNumber); + await requestContext.SendResult(response); } } @@ -139,31 +123,23 @@ namespace Microsoft.SqlTools.ServiceLayer.ServerConfigurations public async Task HandleServerConfigListRequest(ServerConfigListRequestParams parameters, RequestContext requestContext) { Logger.Verbose("HandleServerConfigListRequest"); - try + ConnectionInfo connInfo; + ConnectionServiceInstance.TryFindConnection( + parameters.OwnerUri, + out connInfo); + ServerConfigListResponseParams response = new ServerConfigListResponseParams { - ConnectionInfo connInfo; - ConnectionServiceInstance.TryFindConnection( - parameters.OwnerUri, - out connInfo); - ServerConfigListResponseParams response = new ServerConfigListResponseParams - { - }; + }; - if (connInfo == null) - { - await requestContext.SendError(new Exception(SR.ProfilerConnectionNotFound)); - } - else - { - var serverConnection = ConnectionService.OpenServerConnection(connInfo); - response.ConfigProperties = GetConfigs(serverConnection); - await requestContext.SendResult(response); - } - } - catch (Exception e) + if (connInfo == null) { - // Exception related to run task will be captured here - await requestContext.SendError(e); + await requestContext.SendError(new Exception(SR.ProfilerConnectionNotFound)); + } + else + { + var serverConnection = ConnectionService.OpenServerConnection(connInfo); + response.ConfigProperties = GetConfigs(serverConnection); + await requestContext.SendResult(response); } }