Browse Source

Update multiple code paths to rethrow OperationCanceledException

pull/2560/head
Kévin Chalet 2 months ago
parent
commit
77d82cd7e6
  1. 5
      src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHandlers.cs
  2. 15
      src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHelpers.cs
  3. 7
      src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHttpListener.cs
  4. 10
      src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpHandlers.cs
  5. 12
      src/OpenIddict.Client/OpenIddictClientDispatcher.cs
  6. 48
      src/OpenIddict.Client/OpenIddictClientHandlers.cs
  7. 9
      src/OpenIddict.Core/Managers/OpenIddictApplicationManager.cs
  8. 5
      src/OpenIddict.Core/Managers/OpenIddictAuthorizationManager.cs
  9. 15
      src/OpenIddict.Core/Managers/OpenIddictTokenManager.cs
  10. 12
      src/OpenIddict.Server/OpenIddictServerDispatcher.cs
  11. 2
      src/OpenIddict.Server/OpenIddictServerHandlers.cs
  12. 10
      src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpHandlers.cs
  13. 12
      src/OpenIddict.Validation/OpenIddictValidationDispatcher.cs
  14. 8
      src/OpenIddict.Validation/OpenIddictValidationHandlers.cs

5
src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHandlers.cs

@ -1190,6 +1190,11 @@ public static partial class OpenIddictClientSystemIntegrationHandlers
await _service.RedirectProtocolActivationAsync(activation, identifier, source.Token);
}
catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested)
{
throw;
}
catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception))
{
context.Logger.LogWarning(6215, SR.GetResourceString(SR.ID6215), identifier);

15
src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHelpers.cs

@ -452,6 +452,11 @@ public static class OpenIddictClientSystemIntegrationHelpers
return await UIApplication.SharedApplication.OpenUrlAsync(new NSUrl(uri.AbsoluteUri), new UIApplicationOpenUrlOptions());
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
throw;
}
catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception))
{
return false;
@ -487,6 +492,11 @@ public static class OpenIddictClientSystemIntegrationHelpers
return true;
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
throw;
}
catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception))
{
return false;
@ -521,6 +531,11 @@ public static class OpenIddictClientSystemIntegrationHelpers
return true;
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
throw;
}
catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception))
{
return false;

7
src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHttpListener.cs

@ -202,6 +202,13 @@ public sealed class OpenIddictClientSystemIntegrationHttpListener : BackgroundSe
exceptions.Push(new InvalidOperationException(SR.FormatID0384(port), exception));
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
listener.Close();
throw;
}
catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception))
{
listener.Close();

10
src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpHandlers.cs

@ -477,6 +477,11 @@ public static partial class OpenIddictClientSystemNetHttpHandlers
response = await client.SendAsync(request, HttpCompletionOption.ResponseContentRead, context.CancellationToken);
}
catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested)
{
throw;
}
// If an exception is thrown at this stage, this likely means a persistent network error occurred.
// In this case, log the error details and return a generic error to stop processing the event.
catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception))
@ -723,6 +728,11 @@ public static partial class OpenIddictClientSystemNetHttpHandlers
OpenIddictSerializer.Default.Response, context.CancellationToken);
}
catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested)
{
throw;
}
// If an exception is thrown at this stage, this likely means the returned response was not a valid
// JSON response or was not correctly formatted as a JSON object. This typically happens when
// a server error occurs while the JSON response is being generated and returned to the client.

12
src/OpenIddict.Client/OpenIddictClientDispatcher.cs

@ -39,9 +39,17 @@ public sealed class OpenIddictClientDispatcher : IOpenIddictClientDispatcher
await handler.HandleAsync(context);
}
catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception) && context.Logger.IsEnabled(LogLevel.Debug))
catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested)
{
context.Logger.LogDebug(6132, exception, SR.GetResourceString(SR.ID6132), handler.GetType().FullName, typeof(TContext).FullName);
throw;
}
catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception))
{
if (context.Logger.IsEnabled(LogLevel.Debug))
{
context.Logger.LogDebug(6132, exception, SR.GetResourceString(SR.ID6132), handler.GetType().FullName, typeof(TContext).FullName);
}
throw;
}

48
src/OpenIddict.Client/OpenIddictClientHandlers.cs

@ -530,8 +530,12 @@ public static partial class OpenIddictClientHandlers
?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0140));
}
catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception) &&
exception is not OperationCanceledException)
catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested)
{
throw;
}
catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception))
{
context.Logger.LogError(6219, exception, SR.GetResourceString(SR.ID6219));
@ -1149,8 +1153,12 @@ public static partial class OpenIddictClientHandlers
?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0140));
}
catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception) &&
exception is not OperationCanceledException)
catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested)
{
throw;
}
catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception))
{
context.Logger.LogError(6219, exception, SR.GetResourceString(SR.ID6219));
@ -5030,8 +5038,12 @@ public static partial class OpenIddictClientHandlers
?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0140));
}
catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception) &&
exception is not OperationCanceledException)
catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested)
{
throw;
}
catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception))
{
context.Logger.LogError(6219, exception, SR.GetResourceString(SR.ID6219));
@ -7443,8 +7455,12 @@ public static partial class OpenIddictClientHandlers
?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0140));
}
catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception) &&
exception is not OperationCanceledException)
catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested)
{
throw;
}
catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception))
{
context.Logger.LogError(6219, exception, SR.GetResourceString(SR.ID6219));
@ -8248,8 +8264,12 @@ public static partial class OpenIddictClientHandlers
?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0140));
}
catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception) &&
exception is not OperationCanceledException)
catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested)
{
throw;
}
catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception))
{
context.Logger.LogError(6219, exception, SR.GetResourceString(SR.ID6219));
@ -9031,8 +9051,12 @@ public static partial class OpenIddictClientHandlers
?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0140));
}
catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception) &&
exception is not OperationCanceledException)
catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested)
{
throw;
}
catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception))
{
context.Logger.LogError(6219, exception, SR.GetResourceString(SR.ID6219));

9
src/OpenIddict.Core/Managers/OpenIddictApplicationManager.cs

@ -1407,6 +1407,11 @@ public class OpenIddictApplicationManager<TApplication> : IOpenIddictApplication
await UpdateAsync(application, secret, cancellationToken);
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
throw;
}
catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception))
{
// If a non-fatal exception is thrown, ignore it: the client secret will be updated the next time it is validated again.
@ -1569,7 +1574,7 @@ public class OpenIddictApplicationManager<TApplication> : IOpenIddictApplication
return false;
}
catch (CryptographicException exception) when (!OpenIddictHelpers.IsFatal(exception))
catch (CryptographicException exception)
{
Logger.LogWarning(6288, exception, SR.GetResourceString(SR.ID6288));
@ -1714,7 +1719,7 @@ public class OpenIddictApplicationManager<TApplication> : IOpenIddictApplication
return true;
}
catch (CryptographicException exception) when (!OpenIddictHelpers.IsFatal(exception))
catch (CryptographicException exception)
{
Logger.LogWarning(6288, exception, SR.GetResourceString(SR.ID6288));

5
src/OpenIddict.Core/Managers/OpenIddictAuthorizationManager.cs

@ -774,6 +774,11 @@ public class OpenIddictAuthorizationManager<TAuthorization> : IOpenIddictAuthori
return true;
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
throw;
}
catch (ConcurrencyException exception)
{
Logger.LogDebug(6165, exception, SR.GetResourceString(SR.ID6165), await Store.GetIdAsync(authorization, cancellationToken));

15
src/OpenIddict.Core/Managers/OpenIddictTokenManager.cs

@ -972,6 +972,11 @@ public class OpenIddictTokenManager<TToken> : IOpenIddictTokenManager where TTok
return true;
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
throw;
}
catch (ConcurrencyException exception)
{
Logger.LogDebug(6169, exception, SR.GetResourceString(SR.ID6169), await Store.GetIdAsync(token, cancellationToken));
@ -1008,6 +1013,11 @@ public class OpenIddictTokenManager<TToken> : IOpenIddictTokenManager where TTok
return true;
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
throw;
}
catch (ConcurrencyException exception)
{
Logger.LogDebug(6172, exception, SR.GetResourceString(SR.ID6172), await Store.GetIdAsync(token, cancellationToken));
@ -1044,6 +1054,11 @@ public class OpenIddictTokenManager<TToken> : IOpenIddictTokenManager where TTok
return true;
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
throw;
}
catch (ConcurrencyException exception)
{
Logger.LogDebug(6175, exception, SR.GetResourceString(SR.ID6175), await Store.GetIdAsync(token, cancellationToken));

12
src/OpenIddict.Server/OpenIddictServerDispatcher.cs

@ -39,9 +39,17 @@ public sealed class OpenIddictServerDispatcher : IOpenIddictServerDispatcher
await handler.HandleAsync(context);
}
catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception) && context.Logger.IsEnabled(LogLevel.Debug))
catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested)
{
context.Logger.LogDebug(6132, exception, SR.GetResourceString(SR.ID6132), handler.GetType().FullName, typeof(TContext).FullName);
throw;
}
catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception))
{
if (context.Logger.IsEnabled(LogLevel.Debug))
{
context.Logger.LogDebug(6132, exception, SR.GetResourceString(SR.ID6132), handler.GetType().FullName, typeof(TContext).FullName);
}
throw;
}

2
src/OpenIddict.Server/OpenIddictServerHandlers.cs

@ -1289,7 +1289,7 @@ public static partial class OpenIddictServerHandlers
}
}
catch (CryptographicException exception) when (!OpenIddictHelpers.IsFatal(exception))
catch (CryptographicException exception)
{
context.Logger.LogWarning(6288, exception, SR.GetResourceString(SR.ID6288));

10
src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpHandlers.cs

@ -473,6 +473,11 @@ public static partial class OpenIddictValidationSystemNetHttpHandlers
response = await client.SendAsync(request, HttpCompletionOption.ResponseContentRead, context.CancellationToken);
}
catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested)
{
throw;
}
// If an exception is thrown at this stage, this likely means a persistent network error occurred.
// In this case, log the error details and return a generic error to stop processing the event.
catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception))
@ -719,6 +724,11 @@ public static partial class OpenIddictValidationSystemNetHttpHandlers
OpenIddictSerializer.Default.Response, context.CancellationToken);
}
catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested)
{
throw;
}
// If an exception is thrown at this stage, this likely means the returned response was not a valid
// JSON response or was not correctly formatted as a JSON object. This typically happens when
// a server error occurs while the JSON response is being generated and returned to the client.

12
src/OpenIddict.Validation/OpenIddictValidationDispatcher.cs

@ -39,9 +39,17 @@ public sealed class OpenIddictValidationDispatcher : IOpenIddictValidationDispat
await handler.HandleAsync(context);
}
catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception) && context.Logger.IsEnabled(LogLevel.Debug))
catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested)
{
context.Logger.LogDebug(6132, exception, SR.GetResourceString(SR.ID6132), handler.GetType().FullName, typeof(TContext).FullName);
throw;
}
catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception))
{
if (context.Logger.IsEnabled(LogLevel.Debug))
{
context.Logger.LogDebug(6132, exception, SR.GetResourceString(SR.ID6132), handler.GetType().FullName, typeof(TContext).FullName);
}
throw;
}

8
src/OpenIddict.Validation/OpenIddictValidationHandlers.cs

@ -172,8 +172,12 @@ public static partial class OpenIddictValidationHandlers
?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0140));
}
catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception) &&
exception is not OperationCanceledException)
catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested)
{
throw;
}
catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception))
{
context.Logger.LogError(6219, exception, SR.GetResourceString(SR.ID6219));

Loading…
Cancel
Save