private Task <RavenJToken> SendRequestInternal(Func <HttpRequestMessage> getRequestMessage, bool readErrorString = true) { if (isRequestSentToServer && Debugger.IsAttached == false) { throw new InvalidOperationException("Request was already sent to the server, cannot retry request."); } isRequestSentToServer = true; return(RunWithAuthRetry(async() => { try { var requestMessage = getRequestMessage(); CopyHeadersToHttpRequestMessage(requestMessage); var sp = Stopwatch.StartNew(); try { Response = await httpClient.SendAsync(requestMessage).ConfigureAwait(false); } catch (OperationCanceledException e) { throw new TimeoutException(string.Format("Request for {0} timed out after {1:#,#;;0} ms", requestMessage.RequestUri, sp.ElapsedMilliseconds), e); } SetResponseHeaders(Response); AssertServerVersionSupported(); ResponseStatusCode = Response.StatusCode; } catch (HttpRequestException e) { if (Response == null) //something bad happened and httpClient.SendAsync failed -> i.e. server down, network down { e.Data.Add(Constants.RequestFailedExceptionMarker, true); } throw ErrorResponseException.FromHttpRequestException(e); } finally { sp.Stop(); if (requestTimeMetric != null) { requestTimeMetric.Update((int)sp.Elapsed.TotalMilliseconds); } } // throw the conflict exception return await CheckForErrorsAndReturnCachedResultIfAnyAsync(readErrorString).ConfigureAwait(false); })); }