mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-04 09:35:37 -05:00
Send a Object Explorer session disconnect message on socket exceptions (#739)
* WIP * WIP 2 * Send disconnect message o binding exception * Add a try catch around the binding queue error handler
This commit is contained in:
@@ -5,12 +5,15 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SqlTools.Utility;
|
||||
using System.Linq;
|
||||
using Microsoft.SqlTools.ServiceLayer.Utility;
|
||||
using System.Diagnostics;
|
||||
using Microsoft.SqlTools.Utility;
|
||||
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
||||
{
|
||||
@@ -33,6 +36,10 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
||||
|
||||
private Task queueProcessorTask;
|
||||
|
||||
public delegate void UnhandledExceptionDelegate(string connectionKey, Exception ex);
|
||||
|
||||
public event UnhandledExceptionDelegate OnUnhandledException;
|
||||
|
||||
/// <summary>
|
||||
/// Map from context keys to binding context instances
|
||||
/// Internal for testing purposes only
|
||||
@@ -351,11 +358,28 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
||||
Logger.Write(TraceEventType.Error, "Unexpected exception on the binding queue: " + ex.ToString());
|
||||
if (queueItem.ErrorHandler != null)
|
||||
{
|
||||
result = queueItem.ErrorHandler(ex);
|
||||
try
|
||||
{
|
||||
result = queueItem.ErrorHandler(ex);
|
||||
}
|
||||
catch (Exception ex2)
|
||||
{
|
||||
Logger.Write(TraceEventType.Error, "Unexpected exception in binding queue error handler: " + ex2.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
if (IsExceptionOfType(ex, typeof(SqlException)) || IsExceptionOfType(ex, typeof(SocketException)))
|
||||
{
|
||||
if (this.OnUnhandledException != null)
|
||||
{
|
||||
this.OnUnhandledException(queueItem.Key, ex);
|
||||
}
|
||||
|
||||
RemoveBindingContext(queueItem.Key);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Task.Run(() =>
|
||||
{
|
||||
try
|
||||
@@ -371,7 +395,7 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
||||
|
||||
// if the task didn't complete then call the timeout callback
|
||||
if (queueItem.TimeoutOperation != null)
|
||||
{
|
||||
{
|
||||
queueItem.Result = queueItem.TimeoutOperation(bindingContext);
|
||||
}
|
||||
|
||||
@@ -469,5 +493,10 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsExceptionOfType(Exception ex, Type t)
|
||||
{
|
||||
return ex.GetType() == t || (ex.InnerException != null && ex.InnerException.GetType() == t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,9 +88,8 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
||||
/// Generate a unique key based on the ConnectionInfo object
|
||||
/// </summary>
|
||||
/// <param name="connInfo"></param>
|
||||
private string GetConnectionContextKey(ConnectionInfo connInfo)
|
||||
{
|
||||
ConnectionDetails details = connInfo.ConnectionDetails;
|
||||
internal static string GetConnectionContextKey(ConnectionDetails details)
|
||||
{
|
||||
string key = string.Format("{0}_{1}_{2}_{3}",
|
||||
details.ServerName ?? "NULL",
|
||||
details.DatabaseName ?? "NULL",
|
||||
@@ -108,7 +107,7 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
||||
key += "_" + details.GroupId;
|
||||
}
|
||||
|
||||
return key;
|
||||
return Uri.EscapeUriString(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -158,7 +157,7 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
||||
|
||||
public void RemoveBindigContext(ConnectionInfo connInfo)
|
||||
{
|
||||
string connectionKey = GetConnectionContextKey(connInfo);
|
||||
string connectionKey = GetConnectionContextKey(connInfo.ConnectionDetails);
|
||||
if (BindingContextExists(connectionKey))
|
||||
{
|
||||
RemoveBindingContext(connectionKey);
|
||||
@@ -178,7 +177,7 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
||||
}
|
||||
|
||||
// lookup the current binding context
|
||||
string connectionKey = GetConnectionContextKey(connInfo);
|
||||
string connectionKey = GetConnectionContextKey(connInfo.ConnectionDetails);
|
||||
if (BindingContextExists(connectionKey))
|
||||
{
|
||||
if (overwrite)
|
||||
|
||||
Reference in New Issue
Block a user