private static async Task <ResponseHolder> DocDbExceptionWrapper(Func <ResponseHolder, Task> action) { var response = new ResponseHolder(); try { await action(response); } catch (DocumentClientException dex) { response.StatusCode = (int)dex.StatusCode; response.IsSuccess = false; if (response.StatusCode == 429) { response.ThrottleHasWaited = true; response.ThrottleSuggestedWait = dex.RetryAfter; } response.ResourceCharge = dex.RequestCharge; } catch (Exception ex) { response.Ex = ex; response.StatusCode = 500; response.IsSuccess = false; } return(response); }
/// <summary> /// This method resolves the documentId, and etag for the document in DocumentDb /// </summary> /// <param name="field"></param> /// <param name="id">The entity Id.</param> /// <param name="timeout"></param> /// <param name="extractionJPaths"></param> /// <returns>Returns a response holder that holds teh data.</returns> public async Task <ResponseHolder> ResolveDocumentId(string field, string id, TimeSpan?timeout = null , IEnumerable <KeyValuePair <string, string> > extractionJPaths = null) { var sql = $"SELECT * FROM c WHERE (c.{field}=@id)"; var param = new SearchParameter() { Name = "id", Value = id }; var search = await Search(sql, 1, timeout, null, null, param); var holder = new ResponseHolder { IsTimeout = search.IsTimeout }; if (search.IsSuccess) { var jQuery = JObject.Parse(search.Content); int count = jQuery["_count"].Value <int>(); if (count == 1) { JToken entity = jQuery["Documents"].First(); holder.Id = id; holder.IsSuccess = true; holder.DocumentId = entity["_rid"].Value <string>(); holder.ETag = entity["_etag"].Value <string>(); holder.Content = entity.ToString(); //Extract the additional fields specified in the extraction collection. if (extractionJPaths != null) { foreach (var jpath in extractionJPaths) { var token = entity[jpath.Value]; if (token != null) { holder.Fields.Add(jpath.Key, token.Value <string>()); } } } } else { holder.IsSuccess = false; holder.Response = new HttpResponseMessage(HttpStatusCode.NotFound); } } else { holder.Ex = search.Ex; holder.Response = search.Response; } return(holder); }