Skip to content

Commit 7fa1dee

Browse files
Add more info to encryption related connection failures (#557)
1 parent 089bb65 commit 7fa1dee

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

src/SqlAsyncCollector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public SqlAsyncCollector(IConfiguration configuration, SqlAttribute attribute, I
9595
using (SqlConnection connection = BuildConnection(attribute.ConnectionStringSetting, configuration))
9696
{
9797
this._logger.LogDebugWithThreadId("BEGIN OpenSqlAsyncCollectorVerifyDatabaseSupportedConnection");
98-
connection.Open();
98+
connection.OpenAsyncWithSqlErrorHandling(CancellationToken.None).Wait();
9999
this._logger.LogDebugWithThreadId("END OpenSqlAsyncCollectorVerifyDatabaseSupportedConnection");
100100
VerifyDatabaseSupported(connection, logger, CancellationToken.None).Wait();
101101
}

src/SqlBindingUtilities.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,5 +203,35 @@ public static async Task VerifyDatabaseSupported(SqlConnection connection, ILogg
203203
}
204204
}
205205
}
206+
207+
/// <summary>
208+
/// Opens a connection and handles some specific errors if they occur.
209+
/// </summary>
210+
/// <param name="connection">The connection to open</param>
211+
/// <param name="cancellationToken">The cancellation token to pass to the OpenAsync call</param>
212+
/// <returns>The task that will be completed when the connection is made</returns>
213+
/// <exception cref="InvalidOperationException">Thrown if an error occurred that we want to wrap with more information</exception>
214+
internal static async Task OpenAsyncWithSqlErrorHandling(this SqlConnection connection, CancellationToken cancellationToken)
215+
{
216+
try
217+
{
218+
await connection.OpenAsync(cancellationToken);
219+
}
220+
catch (Exception e)
221+
{
222+
SqlException sqlEx = e is AggregateException a ? a.InnerExceptions.OfType<SqlException>().First() :
223+
e is SqlException s ? s :
224+
null;
225+
// Error number for:
226+
// A connection was successfully established with the server, but then an error occurred during the login process.
227+
// The certificate chain was issued by an authority that is not trusted.
228+
// Add on some more information to help the user figure out how to solve it
229+
if (sqlEx?.Number == -2146893019)
230+
{
231+
throw new InvalidOperationException("The default values for encryption on connections have been changed, please review your configuration to ensure you have the correct values for your server. See https://aka.ms/afsqlext-connection for more details.", e);
232+
}
233+
throw;
234+
}
235+
}
206236
}
207237
}

src/SqlConverters.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public virtual async Task<string> BuildItemFromAttributeAsync(SqlAttribute attri
180180
{
181181
adapter.SelectCommand = command;
182182
this._logger.LogDebugWithThreadId("BEGIN OpenBuildItemFromAttributeAsyncConnection");
183-
await connection.OpenAsync();
183+
await connection.OpenAsyncWithSqlErrorHandling(CancellationToken.None);
184184
this._logger.LogDebugWithThreadId("END OpenBuildItemFromAttributeAsyncConnection");
185185
Dictionary<TelemetryPropertyName, string> props = connection.AsConnectionProps();
186186
TelemetryInstance.TrackConvert(type, props);

src/TriggerBinding/SqlTriggerListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public async Task StartAsync(CancellationToken cancellationToken)
122122
using (var connection = new SqlConnection(this._connectionString))
123123
{
124124
this._logger.LogDebugWithThreadId("BEGIN OpenListenerConnection");
125-
await connection.OpenAsync(cancellationToken);
125+
await connection.OpenAsyncWithSqlErrorHandling(cancellationToken);
126126
this._logger.LogDebugWithThreadId("END OpenListenerConnection");
127127
this._telemetryProps.AddConnectionProps(connection);
128128

0 commit comments

Comments
 (0)