| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Threading; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | |
| | | 7 | | namespace System.Net.Http |
| | | 8 | | { |
| | | 9 | | /// <summary>Provides utilities related to cancellation.</summary> |
| | | 10 | | internal static class CancellationHelper |
| | | 11 | | { |
| | | 12 | | /// <summary>The default message used by <see cref="OperationCanceledException"/>.</summary> |
| | 0 | 13 | | private static readonly string s_cancellationMessage = new OperationCanceledException().Message; // use same mes |
| | | 14 | | |
| | | 15 | | /// <summary>Determines whether to wrap an <see cref="Exception"/> in a cancellation exception.</summary> |
| | | 16 | | /// <param name="exception">The exception.</param> |
| | | 17 | | /// <param name="cancellationToken">The <see cref="CancellationToken"/> that may have triggered the exception.</ |
| | | 18 | | /// <returns>true if the exception should be wrapped; otherwise, false.</returns> |
| | | 19 | | internal static bool ShouldWrapInOperationCanceledException(Exception exception, CancellationToken cancellationT |
| | 0 | 20 | | !(exception is OperationCanceledException) && cancellationToken.IsCancellationRequested; |
| | | 21 | | |
| | | 22 | | /// <summary>Creates a cancellation exception.</summary> |
| | | 23 | | /// <param name="innerException">The inner exception to wrap. May be null.</param> |
| | | 24 | | /// <param name="cancellationToken">The <see cref="CancellationToken"/> that triggered the cancellation.</param> |
| | | 25 | | /// <returns>The cancellation exception.</returns> |
| | | 26 | | internal static Exception CreateOperationCanceledException(Exception? innerException, CancellationToken cancella |
| | 0 | 27 | | new TaskCanceledException(s_cancellationMessage, innerException, cancellationToken); // TCE for compatibilit |
| | | 28 | | |
| | | 29 | | /// <summary>Throws a cancellation exception.</summary> |
| | | 30 | | /// <param name="innerException">The inner exception to wrap. May be null.</param> |
| | | 31 | | /// <param name="cancellationToken">The <see cref="CancellationToken"/> that triggered the cancellation.</param> |
| | | 32 | | private static void ThrowOperationCanceledException(Exception? innerException, CancellationToken cancellationTok |
| | 0 | 33 | | throw CreateOperationCanceledException(innerException, cancellationToken); |
| | | 34 | | |
| | | 35 | | /// <summary>Throws a cancellation exception if cancellation has been requested via <paramref name="cancellation |
| | | 36 | | /// <param name="cancellationToken">The token to check for a cancellation request.</param> |
| | | 37 | | internal static void ThrowIfCancellationRequested(CancellationToken cancellationToken) |
| | 0 | 38 | | { |
| | 0 | 39 | | ThrowIfCancellationRequested(innerException: null, cancellationToken); |
| | 0 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary>Throws a cancellation exception if cancellation has been requested via <paramref name="cancellation |
| | | 43 | | /// <param name="innerException">The inner exception to wrap. May be null.</param> |
| | | 44 | | /// <param name="cancellationToken">The token to check for a cancellation request.</param> |
| | | 45 | | internal static void ThrowIfCancellationRequested(Exception? innerException, CancellationToken cancellationToken |
| | 0 | 46 | | { |
| | 0 | 47 | | if (cancellationToken.IsCancellationRequested) |
| | 0 | 48 | | { |
| | 0 | 49 | | ThrowOperationCanceledException(innerException, cancellationToken); |
| | 0 | 50 | | } |
| | 0 | 51 | | } |
| | | 52 | | } |
| | | 53 | | } |