Skip to content

Commit

Permalink
Intercepters: Don't return null for Async methods
Browse files Browse the repository at this point in the history
Using interceptor?. pattern sent null back for Task which was being awaited.
Pattern, for async always make null check on interceptor before calling into.
For Sync methods if they are returning void or possible null then only use interceptor?.

Testing: Errors only happen when diagnosticSource enabled so that intercepter is null but we still go inside the block.
Not sure how to set it up that in tests. Submitting PR to unblock ASP.NET Core
  • Loading branch information
smitpatel committed Jun 28, 2019
1 parent f9edba9 commit d7b013c
Showing 1 changed file with 32 additions and 35 deletions.
67 changes: 32 additions & 35 deletions src/EFCore.Relational/Diagnostics/RelationalLoggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ public static class RelationalLoggerExtensions
definition,
diagnosticSourceEnabled);

if (interceptor != null)
{
return interceptor.ReaderExecuting(command, eventData, null);
}
return interceptor?.ReaderExecuting(command, eventData, null);
}

return null;
Expand Down Expand Up @@ -127,10 +124,7 @@ public static class RelationalLoggerExtensions
definition,
diagnosticSourceEnabled);

if (interceptor != null)
{
return interceptor.ScalarExecuting(command, eventData, null);
}
return interceptor?.ScalarExecuting(command, eventData, null);
}

return null;
Expand Down Expand Up @@ -176,10 +170,7 @@ public static class RelationalLoggerExtensions
definition,
diagnosticSourceEnabled);

if (interceptor != null)
{
return interceptor.NonQueryExecuting(command, eventData, null);
}
return interceptor?.NonQueryExecuting(command, eventData, null);
}

return null;
Expand Down Expand Up @@ -1006,7 +997,7 @@ private static string CommandError(EventDefinitionBase definition, EventData pay
/// <param name="connection"> The connection. </param>
/// <param name="startTime"> The time that the operation was started. </param>
/// <returns> The result of execution, which may have been modified by an interceptor. </returns>
public static InterceptionResult? ConnectionOpening(
public static InterceptionResult? ConnectionOpening(
[NotNull] this IDiagnosticsLogger<DbLoggerCategory.Database.Connection> diagnostics,
[NotNull] IRelationalConnection connection,
DateTimeOffset startTime)
Expand All @@ -1029,10 +1020,7 @@ private static string CommandError(EventDefinitionBase definition, EventData pay
false,
diagnosticSourceEnabled);

if (interceptor != null)
{
return interceptor.ConnectionOpening(connection.DbConnection, eventData, null);
}
return interceptor?.ConnectionOpening(connection.DbConnection, eventData, null);
}

return null;
Expand Down Expand Up @@ -1418,10 +1406,7 @@ public static void ConnectionClosed(
definition,
diagnosticSourceEnabled);

if (interceptor != null)
{
interceptor.ConnectionClosed(connection.DbConnection, eventData);
}
interceptor?.ConnectionClosed(connection.DbConnection, eventData);
}
}

Expand Down Expand Up @@ -1559,10 +1544,7 @@ public static void ConnectionError(
definition,
diagnosticSourceEnabled);

if (interceptor != null)
{
interceptor.ConnectionFailed(connection.DbConnection, eventData);
}
interceptor?.ConnectionFailed(connection.DbConnection, eventData);
}
}

Expand Down Expand Up @@ -1706,10 +1688,7 @@ private static string ConnectionError(EventDefinitionBase definition, EventData
definition,
diagnosticSourceEnabled);

if (interceptor != null)
{
return interceptor.TransactionStarting(connection.DbConnection, eventData, null);
}
return interceptor?.TransactionStarting(connection.DbConnection, eventData, null);
}

return null;
Expand Down Expand Up @@ -1753,7 +1732,10 @@ private static string ConnectionError(EventDefinitionBase definition, EventData
definition,
diagnosticSourceEnabled);

return interceptor?.TransactionStartingAsync(connection.DbConnection, eventData, null, cancellationToken);
if (interceptor != null)
{
return interceptor.TransactionStartingAsync(connection.DbConnection, eventData, null, cancellationToken);
}
}

return Task.FromResult<InterceptionResult<DbTransaction>?>(null);
Expand Down Expand Up @@ -2183,7 +2165,10 @@ private static string TransactionUsed(EventDefinitionBase definition, EventData
true,
diagnosticSourceEnabled);

return interceptor?.TransactionCommittingAsync(transaction, eventData, null, cancellationToken);
if (interceptor != null)
{
return interceptor.TransactionCommittingAsync(transaction, eventData, null, cancellationToken);
}
}

return Task.FromResult<InterceptionResult?>(null);
Expand Down Expand Up @@ -2311,7 +2296,10 @@ public static Task TransactionCommittedAsync(
true,
diagnosticSourceEnabled);

return interceptor?.TransactionCommittedAsync(transaction, eventData, cancellationToken);
if (interceptor != null)
{
return interceptor.TransactionCommittedAsync(transaction, eventData, cancellationToken);
}
}

return Task.CompletedTask;
Expand Down Expand Up @@ -2441,7 +2429,10 @@ public static Task TransactionRolledBackAsync(
true,
diagnosticSourceEnabled);

return interceptor?.TransactionRolledBackAsync(transaction, eventData, cancellationToken);
if (interceptor != null)
{
return interceptor.TransactionRolledBackAsync(transaction, eventData, cancellationToken);
}
}

return Task.CompletedTask;
Expand Down Expand Up @@ -2568,7 +2559,10 @@ private static void LogTransactionRolledBack(
true,
diagnosticSourceEnabled);

return interceptor?.TransactionRollingBackAsync(transaction, eventData, null, cancellationToken);
if (interceptor != null)
{
return interceptor.TransactionRollingBackAsync(transaction, eventData, null, cancellationToken);
}
}

return Task.FromResult<InterceptionResult?>(null);
Expand Down Expand Up @@ -2747,7 +2741,10 @@ public static Task TransactionErrorAsync(
true,
diagnosticSourceEnabled);

return interceptor?.TransactionFailedAsync(transaction, eventData, cancellationToken);
if (interceptor != null)
{
return interceptor.TransactionFailedAsync(transaction, eventData, cancellationToken);
}
}

return Task.CompletedTask;
Expand Down

0 comments on commit d7b013c

Please sign in to comment.