Ejemplo n.º 1
0
        private static async Task <DocumentClientException> CreateDocumentClientException(HttpResponseMessage responseMessage)
        {
            Stream readStream = await responseMessage.Content.ReadAsStreamAsync();

            Error error = CosmosResource.LoadFrom <Error>(readStream);

            // ensure there is no local ActivityId, since in Gateway mode ActivityId
            // should always come from message headers
            Trace.CorrelationManager.ActivityId = Guid.Empty;

            bool   isNameBased = false;
            bool   isFeed      = false;
            string resourceTypeString;
            string resourceIdOrFullName;

            string resourceLink = responseMessage.RequestMessage.RequestUri.LocalPath;

            if (!PathsHelper.TryParsePathSegments(resourceLink, out isFeed, out resourceTypeString, out resourceIdOrFullName, out isNameBased))
            {
                // if resourceLink is invalid - we will not set resourceAddress in exception.
            }

            return(new DocumentClientException(error,
                                               responseMessage.Headers, responseMessage.StatusCode)
            {
                StatusDescription = responseMessage.ReasonPhrase,
                ResourceAddress = resourceIdOrFullName
            });
        }
Ejemplo n.º 2
0
        private static async Task <DocumentClientException> CreateDocumentClientException(HttpResponseMessage responseMessage)
        {
            // ensure there is no local ActivityId, since in Gateway mode ActivityId
            // should always come from message headers
            Trace.CorrelationManager.ActivityId = Guid.Empty;

            string resourceLink = responseMessage.RequestMessage.RequestUri.LocalPath;

            if (!PathsHelper.TryParsePathSegments(
                    resourceLink,
                    out bool isFeed,
                    out string resourceTypeString,
                    out string resourceIdOrFullName,
                    out bool isNameBased))
            {
                // if resourceLink is invalid - we will not set resourceAddress in exception.
            }

            // If service rejects the initial payload like header is to large it will return an HTML error instead of JSON.
            if (string.Equals(responseMessage.Content?.Headers?.ContentType?.MediaType, ClientExtensions.MediaTypeJson, StringComparison.OrdinalIgnoreCase))
            {
                Stream readStream = await responseMessage.Content.ReadAsStreamAsync();

                Error error = CosmosResource.LoadFrom <Error>(readStream);
                return(new DocumentClientException(
                           error,
                           responseMessage.Headers,
                           responseMessage.StatusCode)
                {
                    StatusDescription = responseMessage.ReasonPhrase,
                    ResourceAddress = resourceIdOrFullName
                });
            }
            else
            {
                string message = responseMessage.Content == null ? null : await responseMessage.Content.ReadAsStringAsync();

                return(new DocumentClientException(
                           message: message,
                           innerException: null,
                           responseHeaders: responseMessage.Headers,
                           statusCode: responseMessage.StatusCode,
                           requestUri: responseMessage.RequestMessage.RequestUri)
                {
                    StatusDescription = responseMessage.ReasonPhrase,
                    ResourceAddress = resourceIdOrFullName
                });
            }
        }