private async Task <HttpResponseMessage> GetHttpResponse(string baseUri, ElasticRequestParams eparams, string json) { HttpResponseMessage response = null; var indexUri = $"{baseUri}/{_indexName}"; var policy = GetOrCreatePolicy(indexUri); var client = CreateClient(baseUri); try { response = await SendAsync(client, eparams.Verb, policy, eparams.GetUri(), json); } catch (HttpRequestException ex) { _logger.Error(ex, "Elastic connection error for {indexUri}", indexUri); _exceptions.Add(ex); } catch (BrokenCircuitException ex) { _logger.Error(ex, "Circuit broken for {indexUri}", indexUri); _exceptions.Add(ex); } catch (OperationCanceledException ex) { _logger.Info(ex, "Elastic connection timeout for {indexUri}", indexUri); _exceptions.Add(ex); } catch (Exception ex) { _logger.Info(ex, "Unexpected exception for {indexUri}", indexUri); _exceptions.Add(ex); } return(response); }
private async Task <string> QueryAsync(ElasticRequestParams eparams, string json) { var randomIndexes = GetRandomIndexes(); _exceptions.Clear(); foreach (var index in randomIndexes) { var baseUri = _uris[index]; if (_logger.IsTraceEnabled) { _logger.Trace().Message("Processing request to Elastic") .Property("baseUri", baseUri) .Property("searchParams", eparams) .Property("json", json) .Write(); } var response = await GetHttpResponse(baseUri, eparams, json); if (response == null) { _logger.Trace("Response is null"); continue; } var result = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { return(result); } var message = !string.IsNullOrEmpty(result) ? result : response.ReasonPhrase; HandleErrorResult(response.StatusCode, message); if (response.StatusCode == HttpStatusCode.NotFound) { _logger.Trace("Not found received"); if (eparams.ThrowNotFound) { break; } return("Not Found"); } } throw GetElasticClientException(eparams, json); }
private Exception GetElasticClientException(ElasticRequestParams eparams, string json) { var resultEx = (_exceptions.Count > 1) ? new AggregateException("No successful results", _exceptions) : _exceptions.FirstOrDefault() ?? new ApplicationException("Normally shouldn't be thrown"); throw new ElasticClientException("Elastic call failed: " + resultEx.Message, resultEx) { Request = json, BaseUrls = _uris, ElasticRequestParams = eparams }; }