コード例 #1
0
        internal static ResponseMessage ToCosmosResponseMessage(this DocumentClientException dce, RequestMessage request)
        {
            // if StatusCode is null it is a client business logic error and it never hit the backend, so throw
            if (dce.StatusCode == null)
            {
                throw dce;
            }

            // if there is a status code then it came from the backend, return error as http error instead of throwing the exception
            ResponseMessage cosmosResponse     = new ResponseMessage(dce.StatusCode ?? HttpStatusCode.InternalServerError, request);
            string          reasonPhraseString = string.Empty;

            if (!string.IsNullOrEmpty(dce.Message))
            {
                if (dce.Message.IndexOfAny(Extensions.NewLineCharacters) >= 0)
                {
                    StringBuilder sb = new StringBuilder(dce.Message);
                    sb = sb.Replace("\r", string.Empty);
                    sb = sb.Replace("\n", string.Empty);
                    reasonPhraseString = sb.ToString();
                }
                else
                {
                    reasonPhraseString = dce.Message;
                }
            }

            cosmosResponse.ErrorMessage = reasonPhraseString;
            cosmosResponse.Error        = dce.Error;

            if (dce.Headers != null)
            {
                foreach (string header in dce.Headers.AllKeys())
                {
                    cosmosResponse.Headers.Add(header, dce.Headers[header]);
                }
            }

            if (request != null)
            {
                request.Properties.Remove(nameof(DocumentClientException));
                request.Properties.Add(nameof(DocumentClientException), dce);
            }

            return(cosmosResponse);
        }
コード例 #2
0
        internal static ResponseMessage ToCosmosResponseMessage(this StoreResponse response, RequestMessage request)
        {
            // Is status code conversion lossy?
            ResponseMessage httpResponse = new ResponseMessage((HttpStatusCode)response.Status, request);

            if (response.ResponseBody != null)
            {
                httpResponse.Content = response.ResponseBody;
            }

            for (int i = 0; i < response.ResponseHeaderNames.Length; i++)
            {
                httpResponse.Headers.Add(response.ResponseHeaderNames[i], response.ResponseHeaderValues[i]);
            }

            return(httpResponse);
        }
コード例 #3
0
 private static void AddHeadersToRequestMessage(RequestMessage requestMessage, string partitionKeyRangeId)
 {
     requestMessage.Headers.PartitionKeyRangeId = partitionKeyRangeId;
     requestMessage.Headers.Add(HttpConstants.HttpHeaders.ShouldBatchContinueOnError, bool.TrueString);
     requestMessage.Headers.Add(HttpConstants.HttpHeaders.IsBatchRequest, bool.TrueString);
 }
コード例 #4
0
        internal static ResponseMessage ToCosmosResponseMessage(this DocumentServiceResponse response, RequestMessage requestMessage)
        {
            Debug.Assert(requestMessage != null, nameof(requestMessage));

            ResponseMessage cosmosResponse = new ResponseMessage(response.StatusCode, requestMessage);

            if (response.ResponseBody != null)
            {
                cosmosResponse.Content = response.ResponseBody;
            }

            if (response.Headers != null)
            {
                foreach (string key in response.Headers)
                {
                    cosmosResponse.Headers.Add(key, response.Headers[key]);
                }
            }

            return(cosmosResponse);
        }
 internal static void PopulateMaxIntegratedCacheStalenessOption(DedicatedGatewayRequestOptions dedicatedGatewayRequestOptions, RequestMessage request)
 {
     if (dedicatedGatewayRequestOptions?.MaxIntegratedCacheStaleness != null)
     {
         request.Headers.Set(HttpConstants.HttpHeaders.DedicatedGatewayPerRequestCacheStaleness, dedicatedGatewayRequestOptions.MaxIntegratedCacheStaleness.Value.ToString("c", CultureInfo.InvariantCulture));
     }
 }
コード例 #6
0
        internal static ResponseMessage ToCosmosResponseMessage(this DocumentServiceResponse response, RequestMessage requestMessage)
        {
            Debug.Assert(requestMessage != null, nameof(requestMessage));

            ResponseMessage cosmosResponse = new ResponseMessage(response.StatusCode, requestMessage);

            if (response.ResponseBody != null)
            {
                cosmosResponse.Content = response.ResponseBody;
            }

            if (response.Headers != null)
            {
                foreach (string key in response.Headers)
                {
                    cosmosResponse.Headers.Add(key, response.Headers[key]);
                }
            }

            CosmosClientSideRequestStatistics cosmosClientSideRequestStatistics = response.RequestStats as CosmosClientSideRequestStatistics;

            cosmosResponse.Diagnostics = new PointOperationStatistics(
                activityId: cosmosResponse.Headers.ActivityId,
                statusCode: response.StatusCode,
                subStatusCode: response.SubStatusCode,
                requestCharge: cosmosResponse.Headers.RequestCharge,
                errorMessage: cosmosResponse.ErrorMessage,
                method: requestMessage?.Method,
                requestUri: requestMessage?.RequestUri,
                clientSideRequestStatistics: cosmosClientSideRequestStatistics);

            return(cosmosResponse);
        }
コード例 #7
0
        internal static void PopulateMaxIntegratedCacheStalenessOption(DedicatedGatewayRequestOptions dedicatedGatewayRequestOptions, RequestMessage request)
        {
            if (dedicatedGatewayRequestOptions?.MaxIntegratedCacheStaleness != null)
            {
                double cacheStalenessInMilliseconds = (double)dedicatedGatewayRequestOptions.MaxIntegratedCacheStaleness.Value.TotalMilliseconds;

                if (cacheStalenessInMilliseconds < 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(DedicatedGatewayRequestOptions.MaxIntegratedCacheStaleness), "MaxIntegratedCacheStaleness cannot be negative.");
                }

                request.Headers.Set(HttpConstants.HttpHeaders.DedicatedGatewayPerRequestCacheStaleness, cacheStalenessInMilliseconds.ToString(CultureInfo.InvariantCulture));
            }
        }
コード例 #8
0
        internal static ResponseMessage ToCosmosResponseMessage(this DocumentServiceResponse documentServiceResponse, RequestMessage requestMessage)
        {
            Debug.Assert(requestMessage != null, nameof(requestMessage));
            Headers headers = documentServiceResponse.Headers.ToCosmosHeaders();

            // Only record point operation stats if ClientSideRequestStats did not record the response.
            if (!(documentServiceResponse.RequestStats is CosmosClientSideRequestStatistics clientSideRequestStatistics) ||
                (clientSideRequestStatistics.ContactedReplicas.Count == 0 && clientSideRequestStatistics.FailedReplicas.Count == 0))
            {
                requestMessage.DiagnosticsContext.AddDiagnosticsInternal(new PointOperationStatistics(
                                                                             activityId: headers.ActivityId,
                                                                             responseTimeUtc: DateTime.UtcNow,
                                                                             statusCode: documentServiceResponse.StatusCode,
                                                                             subStatusCode: documentServiceResponse.SubStatusCode,
                                                                             requestCharge: headers.RequestCharge,
                                                                             errorMessage: null,
                                                                             method: requestMessage?.Method,
                                                                             requestUri: requestMessage?.RequestUriString,
                                                                             requestSessionToken: requestMessage?.Headers?.Session,
                                                                             responseSessionToken: headers.Session));
            }

            // If it's considered a failure create the corresponding CosmosException
            if (!documentServiceResponse.StatusCode.IsSuccess())
            {
                CosmosException cosmosException = CosmosExceptionFactory.Create(
                    documentServiceResponse,
                    headers,
                    requestMessage);

                return(cosmosException.ToCosmosResponseMessage(requestMessage));
            }

            ResponseMessage responseMessage = new ResponseMessage(
                statusCode: documentServiceResponse.StatusCode,
                requestMessage: requestMessage,
                headers: headers,
                cosmosException: null,
                diagnostics: requestMessage.DiagnosticsContext)
            {
                Content = documentServiceResponse.ResponseBody
            };

            return(responseMessage);
        }
コード例 #9
0
        internal static ResponseMessage ToCosmosResponseMessage(this DocumentClientException documentClientException, RequestMessage requestMessage)
        {
            CosmosDiagnosticsContext diagnosticsContext = requestMessage?.DiagnosticsContext;

            if (requestMessage != null)
            {
                diagnosticsContext = requestMessage.DiagnosticsContext;

                if (diagnosticsContext == null)
                {
                    throw new ArgumentNullException("Request message should contain a DiagnosticsContext");
                }
            }
            else
            {
                diagnosticsContext = new CosmosDiagnosticsContextCore();
            }

            CosmosException cosmosException = CosmosExceptionFactory.Create(
                documentClientException,
                diagnosticsContext);

            PointOperationStatistics pointOperationStatistics = new PointOperationStatistics(
                activityId: cosmosException.Headers.ActivityId,
                statusCode: cosmosException.StatusCode,
                subStatusCode: (int)SubStatusCodes.Unknown,
                responseTimeUtc: DateTime.UtcNow,
                requestCharge: cosmosException.Headers.RequestCharge,
                errorMessage: documentClientException.ToString(),
                method: requestMessage?.Method,
                requestUri: requestMessage?.RequestUriString,
                requestSessionToken: requestMessage?.Headers?.Session,
                responseSessionToken: cosmosException.Headers.Session);

            diagnosticsContext.AddDiagnosticsInternal(pointOperationStatistics);

            // if StatusCode is null it is a client business logic error and it never hit the backend, so throw
            if (documentClientException.StatusCode == null)
            {
                throw cosmosException;
            }

            // if there is a status code then it came from the backend, return error as http error instead of throwing the exception
            ResponseMessage responseMessage = cosmosException.ToCosmosResponseMessage(requestMessage);

            if (requestMessage != null)
            {
                requestMessage.Properties.Remove(nameof(DocumentClientException));
                requestMessage.Properties.Add(nameof(DocumentClientException), documentClientException);
            }

            return(responseMessage);
        }