internal static async Task <DocumentClientException> CreateDocumentClientExceptionAsync(HttpResponseMessage responseMessage) { // 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. } // 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, "application/json", StringComparison.OrdinalIgnoreCase)) { Stream readStream = await responseMessage.Content.ReadAsStreamAsync(); Error error = Resource.LoadFrom <Error>(readStream); return(new DocumentClientException( error, responseMessage.Headers, responseMessage.StatusCode) { StatusDescription = responseMessage.ReasonPhrase, ResourceAddress = resourceIdOrFullName }); } else { String message = 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 }); } }
internal string GetResourceAddress() { string resourceIdOrFullName; bool isNameBased = false; bool isFeed = false; string resourceTypeString; string resourceLink = this.RequestMessage?.RequestUri.OriginalString; if (PathsHelper.TryParsePathSegments(resourceLink, out isFeed, out resourceTypeString, out resourceIdOrFullName, out isNameBased)) { Debug.Assert(resourceIdOrFullName != null); return(resourceIdOrFullName); } return(null); }
private static string GetSessionToken(SessionContainerState self, string collectionLink) { bool arePathSegmentsParsed = PathsHelper.TryParsePathSegments( collectionLink, out _, out _, out string resourceIdOrFullName, out bool isNameBased); ConcurrentDictionary <string, ISessionToken> partitionKeyRangeIdToTokenMap = null; if (arePathSegmentsParsed) { ulong?maybeRID = null; if (isNameBased) { string collectionName = PathsHelper.GetCollectionPath(resourceIdOrFullName); if (self.collectionNameByResourceId.TryGetValue(collectionName, out ulong rid)) { maybeRID = rid; } } else { ResourceId resourceId = ResourceId.Parse(resourceIdOrFullName); if (resourceId.DocumentCollection != 0) { maybeRID = resourceId.UniqueDocumentCollectionId; } } if (maybeRID.HasValue) { self.sessionTokensRIDBased.TryGetValue(maybeRID.Value, out partitionKeyRangeIdToTokenMap); } } if (partitionKeyRangeIdToTokenMap == null) { return(string.Empty); } return(SessionContainer.GetSessionTokenString(partitionKeyRangeIdToTokenMap)); }
internal static async Task <DocumentClientException> CreateDocumentClientExceptionAsync( HttpResponseMessage responseMessage, IClientSideRequestStatistics requestStatistics) { 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. } // 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, "application/json", StringComparison.OrdinalIgnoreCase)) { Stream readStream = await responseMessage.Content.ReadAsStreamAsync(); Error error = Documents.Resource.LoadFrom <Error>(readStream); return(new DocumentClientException( error, responseMessage.Headers, responseMessage.StatusCode) { StatusDescription = responseMessage.ReasonPhrase, ResourceAddress = resourceIdOrFullName, RequestStatistics = requestStatistics }); } else { StringBuilder context = new StringBuilder(); context.AppendLine(await responseMessage.Content.ReadAsStringAsync()); HttpRequestMessage requestMessage = responseMessage.RequestMessage; if (requestMessage != null) { context.AppendLine($"RequestUri: {requestMessage.RequestUri.ToString()};"); context.AppendLine($"RequestMethod: {requestMessage.Method.Method};"); if (requestMessage.Headers != null) { foreach (KeyValuePair <string, IEnumerable <string> > header in requestMessage.Headers) { context.AppendLine($"Header: {header.Key} Length: {string.Join(",", header.Value).Length};"); } } } String message = await responseMessage.Content.ReadAsStringAsync(); return(new DocumentClientException( message: context.ToString(), innerException: null, responseHeaders: responseMessage.Headers, statusCode: responseMessage.StatusCode, requestUri: responseMessage.RequestMessage.RequestUri) { StatusDescription = responseMessage.ReasonPhrase, ResourceAddress = resourceIdOrFullName, RequestStatistics = requestStatistics }); } }