mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-29 17:24:34 -05:00
Fixing bug in GetOrOpenConnection (#234)
Fixes a bug where the GetOrOpenConnection method of the ConnectionService would throw if the connection didn't exist. It would yield a very unhelpful "key not found in collection" exception. The code has been updated to be much more helpful and fix the bug.
This commit is contained in:
@@ -388,25 +388,24 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
|
||||
/// creates a new connection. This cannot be used to create a default connection or to create a
|
||||
/// connection if a default connection does not exist.
|
||||
/// </summary>
|
||||
/// <returns>A DB connection for the connection type requested</returns>
|
||||
public async Task<DbConnection> GetOrOpenConnection(string ownerUri, string connectionType)
|
||||
{
|
||||
if (string.IsNullOrEmpty(ownerUri) || string.IsNullOrEmpty(connectionType))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
Validate.IsNotNullOrEmptyString(nameof(ownerUri), ownerUri);
|
||||
Validate.IsNotNullOrEmptyString(nameof(connectionType), connectionType);
|
||||
|
||||
// Try to get the ConnectionInfo, if it exists
|
||||
ConnectionInfo connectionInfo = ownerToConnectionMap[ownerUri];
|
||||
if (connectionInfo == null)
|
||||
ConnectionInfo connectionInfo;
|
||||
if (!ownerToConnectionMap.TryGetValue(ownerUri, out connectionInfo))
|
||||
{
|
||||
return null;
|
||||
throw new ArgumentOutOfRangeException(SR.ConnectionServiceListDbErrorNotConnected(ownerUri));
|
||||
}
|
||||
|
||||
// Make sure a default connection exists
|
||||
DbConnection defaultConnection;
|
||||
if (!connectionInfo.TryGetConnection(ConnectionType.Default, out defaultConnection))
|
||||
{
|
||||
return null;
|
||||
throw new InvalidOperationException(SR.ConnectionServiceDbErrorDefaultNotConnected(ownerUri));
|
||||
}
|
||||
|
||||
// Try to get the DbConnection
|
||||
@@ -416,7 +415,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
|
||||
// If the DbConnection does not exist and is not the default connection, create one.
|
||||
// We can't create the default (initial) connection here because we won't have a ConnectionDetails
|
||||
// if Connect() has not yet been called.
|
||||
ConnectParams connectParams = new ConnectParams()
|
||||
ConnectParams connectParams = new ConnectParams
|
||||
{
|
||||
OwnerUri = ownerUri,
|
||||
Connection = connectionInfo.ConnectionDetails,
|
||||
|
||||
@@ -730,6 +730,11 @@ namespace Microsoft.SqlTools.ServiceLayer
|
||||
return Keys.GetString(Keys.ConnectionServiceListDbErrorNotConnected, uri);
|
||||
}
|
||||
|
||||
public static string ConnectionServiceDbErrorDefaultNotConnected(string uri)
|
||||
{
|
||||
return Keys.GetString(Keys.ConnectionServiceDbErrorDefaultNotConnected, uri);
|
||||
}
|
||||
|
||||
public static string ConnectionServiceConnStringInvalidAuthType(string authType)
|
||||
{
|
||||
return Keys.GetString(Keys.ConnectionServiceConnStringInvalidAuthType, authType);
|
||||
@@ -802,6 +807,9 @@ namespace Microsoft.SqlTools.ServiceLayer
|
||||
public const string ConnectionServiceListDbErrorNotConnected = "ConnectionServiceListDbErrorNotConnected";
|
||||
|
||||
|
||||
public const string ConnectionServiceDbErrorDefaultNotConnected = "ConnectionServiceDbErrorDefaultNotConnected";
|
||||
|
||||
|
||||
public const string ConnectionServiceConnStringInvalidAuthType = "ConnectionServiceConnStringInvalidAuthType";
|
||||
|
||||
|
||||
|
||||
@@ -128,6 +128,11 @@
|
||||
<data name="ConnectionServiceListDbErrorNotConnected" xml:space="preserve">
|
||||
<value>SpecifiedUri '{0}' does not have existing connection</value>
|
||||
<comment>.
|
||||
Parameters: 0 - uri (string) </comment>
|
||||
</data>
|
||||
<data name="ConnectionServiceDbErrorDefaultNotConnected" xml:space="preserve">
|
||||
<value>Specified URI '{0}' does not have a default connection</value>
|
||||
<comment>.
|
||||
Parameters: 0 - uri (string) </comment>
|
||||
</data>
|
||||
<data name="ConnectionServiceConnStringInvalidAuthType" xml:space="preserve">
|
||||
|
||||
@@ -29,6 +29,8 @@ ConnectionServiceListDbErrorNullOwnerUri = OwnerUri cannot be null or empty
|
||||
|
||||
ConnectionServiceListDbErrorNotConnected(string uri) = SpecifiedUri '{0}' does not have existing connection
|
||||
|
||||
ConnectionServiceDbErrorDefaultNotConnected(string uri) = Specified URI '{0}' does not have a default connection
|
||||
|
||||
ConnectionServiceConnStringInvalidAuthType(string authType) = Invalid value '{0}' for AuthenticationType. Valid values are 'Integrated' and 'SqlLogin'.
|
||||
|
||||
ConnectionServiceConnStringInvalidIntent(string intent) = Invalid value '{0}' for ApplicationIntent. Valid values are 'ReadWrite' and 'ReadOnly'.
|
||||
|
||||
Reference in New Issue
Block a user