Skip to content

Commit

Permalink
Show plugin error details in Azure Data Studio messages
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkMpn committed Nov 6, 2023
1 parent 1ed3d60 commit cab37bd
Showing 1 changed file with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Globalization;
using System.Linq;
using System.Net;
using System.ServiceModel;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
Expand All @@ -18,11 +19,9 @@
using MarkMpn.Sql4Cds.LanguageServer.Workspace;
using Microsoft.SqlTools.ServiceLayer.ExecutionPlan.Contracts;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Metadata;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using StreamJsonRpc;
using static Microsoft.ApplicationInsights.MetricDimensionNames.TelemetryContext;

namespace MarkMpn.Sql4Cds.LanguageServer.QueryExecution
{
Expand Down Expand Up @@ -400,7 +399,7 @@ private async Task Execute(Connection.Session session, ExecuteRequestParamsBase
{
BatchId = batchSummary.Id,
Time = DateTime.UtcNow.ToString("o"),
Message = ex.Message,
Message = GetErrorMessage(ex),
IsError = true
}
});
Expand Down Expand Up @@ -507,6 +506,54 @@ private async Task Execute(Connection.Session session, ExecuteRequestParamsBase
});
}

private string GetErrorMessage(Exception error)
{
string msg;

if (error is AggregateException aggregateException)
msg = String.Join("\r\n", aggregateException.InnerExceptions.Select(ex => GetErrorMessage(ex)));
else
msg = error.Message;

while (error.InnerException != null)
{
if (error.InnerException.Message != error.Message)
msg += "\r\n" + error.InnerException.Message;

error = error.InnerException;
}

if (error is FaultException<OrganizationServiceFault> faultEx)
{
var fault = faultEx.Detail;

if (fault.Message != error.Message)
msg += "\r\n" + fault.Message;

if (fault.ErrorDetails.TryGetValue("Plugin.ExceptionFromPluginExecute", out var plugin))
msg += "\r\nError from plugin: " + plugin;

if (!String.IsNullOrEmpty(fault.TraceText))
msg += "\r\nTrace log: " + fault.TraceText;

while (fault.InnerFault != null)
{
if (fault.InnerFault.Message != fault.Message)
msg += "\r\n" + fault.InnerFault.Message;

fault = fault.InnerFault;

if (fault.ErrorDetails.TryGetValue("Plugin.ExceptionFromPluginExecute", out plugin))
msg += "\r\nError from plugin: " + plugin;

if (!String.IsNullOrEmpty(fault.TraceText))
msg += "\r\nTrace log: " + fault.TraceText;
}
}

return msg;
}

private string GetDisplayName(int count, EntityMetadata meta)
{
if (count == 1)
Expand Down

0 comments on commit cab37bd

Please sign in to comment.