public override async Task <ResponseMessage> ReadNextAsync(CancellationToken cancellationToken)
        {
            // This catches exception thrown by the pipeline and converts it to QueryResponse
            try
            {
                return(await this.ExecuteNextHelperAsync(cancellationToken));
            }
            catch (DocumentClientException exception)
            {
                return(exception.ToCosmosResponseMessage(request: null));
            }
            catch (CosmosException exception)
            {
                return(exception.ToCosmosResponseMessage(request: null));
            }
            catch (AggregateException ae)
            {
                ResponseMessage errorMessage = TransportHandler.AggregateExceptionConverter(ae, null);
                if (errorMessage != null)
                {
                    return(errorMessage);
                }

                throw;
            }
        }
        internal async Task <ThroughputResponse> ReplaceThroughputPropertiesIfExistsAsync(
            string targetRID,
            ThroughputProperties throughputProperties,
            RequestOptions requestOptions,
            CancellationToken cancellationToken = default)
        {
            try
            {
                (ThroughputProperties currentProperty, double requestCharge) = await this.GetOfferV2Async <ThroughputProperties>(targetRID, failIfNotConfigured : false, cancellationToken : cancellationToken);

                if (currentProperty == null)
                {
                    CosmosException notFound = CosmosExceptionFactory.CreateNotFoundException(
                        $"Throughput is not configured for {targetRID}",
                        headers: new Headers()
                    {
                        RequestCharge = requestCharge
                    });
                    return(new ThroughputResponse(
                               httpStatusCode: notFound.StatusCode,
                               headers: notFound.Headers,
                               throughputProperties: null,
                               diagnostics: notFound.Diagnostics,
                               requestMessage: null));
                }

                currentProperty.Content = throughputProperties.Content;

                return(await this.GetThroughputResponseAsync(
                           streamPayload : this.ClientContext.SerializerCore.ToStream(currentProperty),
                           operationType : OperationType.Replace,
                           linkUri : new Uri(currentProperty.SelfLink, UriKind.Relative),
                           resourceType : ResourceType.Offer,
                           currentRequestCharge : requestCharge,
                           requestOptions : requestOptions,
                           cancellationToken : cancellationToken));
            }
            catch (DocumentClientException dce)
            {
                ResponseMessage responseMessage = dce.ToCosmosResponseMessage(null);
                return(new ThroughputResponse(
                           responseMessage.StatusCode,
                           headers: responseMessage.Headers,
                           throughputProperties: null,
                           diagnostics: responseMessage.Diagnostics,
                           requestMessage: responseMessage.RequestMessage));
            }
            catch (AggregateException ex)
            {
                ResponseMessage responseMessage = TransportHandler.AggregateExceptionConverter(ex, null);
                return(new ThroughputResponse(
                           responseMessage.StatusCode,
                           headers: responseMessage.Headers,
                           throughputProperties: null,
                           diagnostics: responseMessage.Diagnostics,
                           requestMessage: responseMessage.RequestMessage));
            }
        }
        public void TestAggregateExceptionConverter()
        {
            string errorMessage = "BadRequest message";
            IEnumerable<Exception> exceptions = new List<Exception>()
            {
                new DocumentClientException(errorMessage, innerException: null, statusCode: HttpStatusCode.BadRequest)
            };

            AggregateException ae = new AggregateException(message: "Test AE message", innerExceptions: exceptions);

            ResponseMessage response = TransportHandler.AggregateExceptionConverter(ae, null);
            Assert.IsNotNull(response);
            Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
            Assert.IsTrue(response.ErrorMessage.Contains(errorMessage));
        }
示例#4
0
        internal async Task <ThroughputResponse> ReplaceThroughputIfExistsAsync(
            string targetRID,
            int throughput,
            RequestOptions requestOptions,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            try
            {
                OfferV2 offerV2 = await this.GetOfferV2Async(targetRID, failIfNotConfigured : true, cancellationToken : cancellationToken);

                OfferV2 newOffer = new OfferV2(offerV2, throughput);

                return(await this.GetThroughputResponseAsync(
                           streamPayload : this.ClientContext.SerializerCore.ToStream(newOffer),
                           operationType : OperationType.Replace,
                           linkUri : new Uri(offerV2.SelfLink, UriKind.Relative),
                           resourceType : ResourceType.Offer,
                           requestOptions : requestOptions,
                           cancellationToken : cancellationToken));
            }
            catch (DocumentClientException dce)
            {
                ResponseMessage responseMessage = dce.ToCosmosResponseMessage(null);
                return(new ThroughputResponse(
                           responseMessage.StatusCode,
                           headers: responseMessage.Headers,
                           throughputProperties: null,
                           diagnostics: responseMessage.Diagnostics));
            }
            catch (AggregateException ex)
            {
                ResponseMessage responseMessage = TransportHandler.AggregateExceptionConverter(ex, null);
                return(new ThroughputResponse(
                           responseMessage.StatusCode,
                           headers: responseMessage.Headers,
                           throughputProperties: null,
                           diagnostics: responseMessage.Diagnostics));
            }
        }
        public override async Task <ResponseMessage> ReadNextAsync(CancellationToken cancellationToken)
        {
            if (this.responseMessageException != null)
            {
                return(this.responseMessageException);
            }

            // This catches exception thrown by the pipeline and converts it to QueryResponse
            ResponseMessage response;

            try
            {
                response = await this.ExecuteNextHelperAsync(cancellationToken);
            }
            catch (DocumentClientException exception)
            {
                response = exception.ToCosmosResponseMessage(request: null);
            }
            catch (CosmosException exception)
            {
                response = exception.ToCosmosResponseMessage(request: null);
            }
            catch (AggregateException ae)
            {
                response = TransportHandler.AggregateExceptionConverter(ae, null);
                if (response == null)
                {
                    throw;
                }
            }

            if (!response.IsSuccessStatusCode)
            {
                this.responseMessageException = response;
            }

            return(response);
        }
示例#6
0
        private async Task <ResponseMessage> QueryRequestExecutorAsync(
            int?maxItemCount,
            string continuationToken,
            RequestOptions options,
            object state,
            CancellationToken cancellationToken)
        {
            // This catches exception thrown by the caches and converts it to QueryResponse
            try
            {
                CosmosQueryExecutionContext cosmosQueryExecution = (CosmosQueryExecutionContext)state;
                return(await cosmosQueryExecution.ExecuteNextAsync(cancellationToken));
            }
            catch (DocumentClientException exception)
            {
                return(exception.ToCosmosResponseMessage(request: null));
            }
            catch (CosmosException exception)
            {
                return(new ResponseMessage(
                           headers: exception.Headers,
                           requestMessage: null,
                           errorMessage: exception.Message,
                           statusCode: exception.StatusCode,
                           error: exception.Error));
            }
            catch (AggregateException ae)
            {
                ResponseMessage errorMessage = TransportHandler.AggregateExceptionConverter(ae, null);
                if (errorMessage != null)
                {
                    return(errorMessage);
                }

                throw;
            }
        }
        public override async Task <ResponseMessage> ReadNextAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            // This catches exception thrown by the pipeline and converts it to QueryResponse
            ResponseMessage response;

            try
            {
                QueryResponseCore responseCore = await this.cosmosQueryExecutionContext.ExecuteNextAsync(cancellationToken);

                CosmosQueryContext cosmosQueryContext = this.cosmosQueryExecutionContext.CosmosQueryContext;
                QueryResponse      queryResponse;
                if (responseCore.IsSuccess)
                {
                    queryResponse = QueryResponse.CreateSuccess(
                        result: responseCore.CosmosElements,
                        count: responseCore.CosmosElements.Count,
                        responseLengthBytes: responseCore.ResponseLengthBytes,
                        queryMetrics: responseCore.QueryMetrics,
                        responseHeaders: new CosmosQueryResponseMessageHeaders(
                            responseCore.ContinuationToken,
                            responseCore.DisallowContinuationTokenMessage,
                            cosmosQueryContext.ResourceTypeEnum,
                            cosmosQueryContext.ContainerResourceId)
                    {
                        RequestCharge = responseCore.RequestCharge,
                        ActivityId    = responseCore.ActivityId
                    });
                }
                else
                {
                    queryResponse = QueryResponse.CreateFailure(
                        statusCode: responseCore.StatusCode,
                        error: null,
                        errorMessage: responseCore.ErrorMessage,
                        requestMessage: null,
                        responseHeaders: new CosmosQueryResponseMessageHeaders(
                            responseCore.ContinuationToken,
                            responseCore.DisallowContinuationTokenMessage,
                            cosmosQueryContext.ResourceTypeEnum,
                            cosmosQueryContext.ContainerResourceId)
                    {
                        RequestCharge = responseCore.RequestCharge,
                        ActivityId    = responseCore.ActivityId
                    });
                }

                if (responseCore.QueryMetrics != null && responseCore.QueryMetrics.Count > 0)
                {
                    queryResponse.Diagnostics = new QueryOperationStatistics(responseCore.QueryMetrics);
                }

                queryResponse.CosmosSerializationOptions = cosmosQueryContext.QueryRequestOptions.CosmosSerializationFormatOptions;

                response = queryResponse;
            }
            catch (Documents.DocumentClientException exception)
            {
                response = exception.ToCosmosResponseMessage(request: null);
            }
            catch (CosmosException exception)
            {
                response = exception.ToCosmosResponseMessage(request: null);
            }
            catch (AggregateException ae)
            {
                response = TransportHandler.AggregateExceptionConverter(ae, null);
                if (response == null)
                {
                    throw;
                }
            }

            return(response);
        }