diff --git a/src/Elastic.Clients.Elasticsearch.Shared/Client/ElasticsearchClient.cs b/src/Elastic.Clients.Elasticsearch.Shared/Client/ElasticsearchClient.cs index 26a611775d..c71b196dec 100644 --- a/src/Elastic.Clients.Elasticsearch.Shared/Client/ElasticsearchClient.cs +++ b/src/Elastic.Clients.Elasticsearch.Shared/Client/ElasticsearchClient.cs @@ -234,15 +234,19 @@ async ValueTask SendRequestWithProductCheckCore() // Evaluate product check result - var productCheckSucceeded = response.ApiCallDetails.TryGetHeader("x-elastic-product", out var values) && - values.FirstOrDefault(x => x.Equals("Elasticsearch", StringComparison.Ordinal)) is not null; + var hasSuccessStatusCode = response.ApiCallDetails.HttpStatusCode is >= 200 and <= 299; + if (hasSuccessStatusCode) + { + var productCheckSucceeded = response.ApiCallDetails.TryGetHeader("x-elastic-product", out var values) && + values.FirstOrDefault(x => x.Equals("Elasticsearch", StringComparison.Ordinal)) is not null; - _productCheckStatus = productCheckSucceeded - ? (int)ProductCheckStatus.Succeeded - : (int)ProductCheckStatus.Failed; + _productCheckStatus = productCheckSucceeded + ? (int)ProductCheckStatus.Succeeded + : (int)ProductCheckStatus.Failed; - if (_productCheckStatus == (int)ProductCheckStatus.Failed) - throw new UnsupportedProductException(UnsupportedProductException.InvalidProductError); + if (_productCheckStatus == (int)ProductCheckStatus.Failed) + throw new UnsupportedProductException(UnsupportedProductException.InvalidProductError); + } if (request.RequestParameters.RequestConfiguration is null) return response; @@ -254,6 +258,13 @@ async ValueTask SendRequestWithProductCheckCore() else if (originalHeaders is { Count: > 0 }) request.RequestParameters.RequestConfiguration.ResponseHeadersToParse = originalHeaders.Value; + if (!hasSuccessStatusCode) + { + // The product check is unreliable for non success status codes. + // We have to re-try on the next request. + _productCheckStatus = (int)ProductCheckStatus.NotChecked; + } + return response; } }