System.Net.Http.HttpClient.HandleFinishSendAsyncError C# (CSharp) Method

HandleFinishSendAsyncError() private method

private HandleFinishSendAsyncError ( Exception e, CancellationTokenSource cts ) : void
e System.Exception
cts System.Threading.CancellationTokenSource
return void
        private void HandleFinishSendAsyncError(Exception e, CancellationTokenSource cts)
        {
            if (NetEventSource.IsEnabled) NetEventSource.Error(this, e);

            // If the cancellation token was canceled, we consider the exception to be caused by the
            // cancellation (e.g. WebException when reading from canceled response stream).
            if (cts.IsCancellationRequested && e is HttpRequestException)
            {
                if (NetEventSource.IsEnabled) NetEventSource.Error(this, $"Canceled");
                throw new OperationCanceledException(cts.Token);
            }
        }

Usage Example

        private async Task <HttpResponseMessage> FinishSendAsyncUnbuffered(
            Task <HttpResponseMessage> sendTask,
            HttpRequestMessage request,
            CancellationTokenSource cts,
            bool disposeCts)
        {
            HttpClient          httpClient = this;
            HttpResponseMessage httpResponseMessage;

            try
            {
                HttpResponseMessage response = await sendTask.ConfigureAwait(false);

                if (response == null)
                {
                    throw new InvalidOperationException(SR.net_http_handler_noresponse);
                }
                if (NetEventSource.IsEnabled)
                {
                    NetEventSource.ClientSendCompleted(httpClient, response, request);
                }
                httpResponseMessage = response;
            }
            catch (Exception ex)
            {
                httpClient.HandleFinishSendAsyncError(ex, cts);
                throw;
            }
            finally
            {
                httpClient.HandleFinishSendAsyncCleanup(cts, disposeCts);
            }
            return(httpResponseMessage);
        }