public ElasticsearchClientException CreateClientException <TResponse>(
            TResponse response, IApiCallDetails callDetails, RequestData data, List <PipelineException> pipelineExceptions
            )
            where TResponse : class, IElasticsearchResponse, new()
        {
            if (callDetails?.Success ?? false)
            {
                return(null);
            }
            var innerException = pipelineExceptions.HasAny() ? pipelineExceptions.AsAggregateOrFirst() : callDetails?.OriginalException;

            var statusCode = callDetails?.HttpStatusCode != null?callDetails.HttpStatusCode.Value.ToString() : "unknown";

            var resource = callDetails == null
                                ? "unknown resource"
                                : $"Status code {statusCode} from: {callDetails.HttpMethod} {callDetails.Uri.PathAndQuery}";


            var exceptionMessage = innerException?.Message ?? $"Request failed to execute";

            var pipelineFailure = data.OnFailurePipelineFailure;

            if (pipelineExceptions.HasAny())
            {
                pipelineFailure = pipelineExceptions.Last().FailureReason;
            }

            if (this.IsTakingTooLong)
            {
                pipelineFailure = PipelineFailure.MaxTimeoutReached;
                this.Audit(MaxTimeoutReached);
                exceptionMessage = "Maximum timeout reached while retrying request";
            }
            else if (this.Retried >= this.MaxRetries && this.MaxRetries > 0)
            {
                pipelineFailure = PipelineFailure.MaxRetriesReached;
                this.Audit(MaxRetriesReached);
                exceptionMessage = "Maximum number of retries reached";
            }

            exceptionMessage += $". Call: {resource}";
            if (response != null && response.TryGetServerErrorReason(out var reason))
            {
                exceptionMessage += $". ServerError: {reason}";
            }


            var clientException = new ElasticsearchClientException(pipelineFailure, exceptionMessage, innerException)
            {
                Request    = data,
                Response   = callDetails,
                AuditTrail = this.AuditTrail
            };

            return(clientException);
        }
Example #2
0
        public void ThrowNoNodesAttempted(RequestData requestData, List <PipelineException> seenExceptions)
        {
            var clientException = new ElasticsearchClientException(PipelineFailure.NoNodesAttempted, NoNodesAttemptedMessage, (Exception)null);

            using (this.Audit(NoNodesAttempted))
                throw new UnexpectedElasticsearchClientException(clientException, seenExceptions)
                      {
                          Request    = requestData,
                          AuditTrail = this.AuditTrail
                      };
        }
Example #3
0
        public void BadResponse <TReturn>(ref ElasticsearchResponse <TReturn> response, RequestData data, List <PipelineException> pipelineExceptions)
            where TReturn : class
        {
            var callDetails     = response ?? pipelineExceptions.LastOrDefault()?.Response;
            var pipelineFailure = PipelineFailure.BadResponse;

            if (pipelineExceptions.HasAny())
            {
                pipelineFailure = pipelineExceptions.Last().FailureReason;
            }

            var innerException = pipelineExceptions.HasAny()
                                ? new AggregateException(pipelineExceptions)
                                : callDetails?.OriginalException;

            var exceptionMessage = innerException?.Message ?? "Could not complete the request to Elasticsearch.";

            if (this.IsTakingTooLong)
            {
                pipelineFailure = PipelineFailure.MaxTimeoutReached;
                this.Audit(MaxTimeoutReached);
                exceptionMessage = "Maximum timout reached while retrying request";
            }
            else if (this.Retried >= this.MaxRetries && this.MaxRetries > 0)
            {
                pipelineFailure = PipelineFailure.MaxRetriesReached;
                this.Audit(MaxRetriesReached);
                exceptionMessage = "Maximum number of retries reached.";
            }

            var clientException = new ElasticsearchClientException(pipelineFailure, exceptionMessage, innerException)
            {
                Request    = data,
                Response   = callDetails,
                AuditTrail = this.AuditTrail
            };

            if (_settings.ThrowExceptions)
            {
                this._settings.OnRequestCompleted?.Invoke(clientException.Response);
                throw clientException;
            }

            if (response == null)
            {
                response = new ResponseBuilder <TReturn>(data)
                {
                    Exception = clientException
                }.ToResponse();
            }

            response.AuditTrail = this.AuditTrail;
        }
 private void HandleElasticsearchClientException(ElasticsearchClientException clientException, IElasticsearchResponse response)
 {
     if (clientException != null && response.ApiCall.OriginalException == null && response.ApiCall is ApiCallDetails a)
     {
         a.OriginalException = clientException;
     }
     this.Settings.OnRequestCompleted?.Invoke(response.ApiCall);
     if (clientException != null && this.Settings.ThrowExceptions)
     {
         throw clientException;
     }
 }
Example #5
0
        public void BadResponse <TResponse>(ref TResponse response, IApiCallDetails callDetails, RequestData data,
                                            ElasticsearchClientException exception
                                            )
            where TResponse : class, IElasticsearchResponse, new()
        {
            if (response == null)
            {
                //make sure we copy over the error body in case we disabled direct streaming.
                var s = callDetails?.ResponseBodyInBytes == null ? Stream.Null : _memoryStreamFactory.Create(callDetails.ResponseBodyInBytes);
                var m = callDetails?.ResponseMimeType ?? RequestData.MimeType;
                response = ResponseBuilder.ToResponse <TResponse>(data, exception, callDetails?.HttpStatusCode, null, s, m);
            }

            response.ApiCall.AuditTrail = AuditTrail;
        }
        public ElasticsearchClientException CreateClientException <TResponse>(
            TResponse response, IApiCallDetails callDetails, RequestData data, List <PipelineException> pipelineExceptions
            )
            where TResponse : class, IElasticsearchResponse, new()
        {
            if (callDetails.Success)
            {
                return(null);
            }
            var innerException = pipelineExceptions.HasAny() ? new AggregateException(pipelineExceptions) : callDetails?.OriginalException;

            var exceptionMessage = innerException?.Message ?? $"Request failed to execute";

            if (response != null && response.TryGetServerErrorReason(out var reason))
            {
                exceptionMessage += $". ServerError: {reason}";
            }

            var pipelineFailure = data.OnFailurePipelineFailure;

            if (pipelineExceptions.HasAny())
            {
                pipelineFailure = pipelineExceptions.Last().FailureReason;
            }

            if (this.IsTakingTooLong)
            {
                pipelineFailure = PipelineFailure.MaxTimeoutReached;
                this.Audit(MaxTimeoutReached);
                exceptionMessage = "Maximum timeout reached while retrying request";
            }
            else if (this.Retried >= this.MaxRetries && this.MaxRetries > 0)
            {
                pipelineFailure = PipelineFailure.MaxRetriesReached;
                this.Audit(MaxRetriesReached);
                exceptionMessage = "Maximum number of retries reached.";
            }

            var clientException = new ElasticsearchClientException(pipelineFailure, exceptionMessage, innerException)
            {
                Request    = data,
                Response   = callDetails,
                AuditTrail = this.AuditTrail
            };

            return(clientException);
        }
Example #7
0
        public ElasticsearchClientException CreateClientException <TResponse>(
            TResponse response, IApiCallDetails callDetails, RequestData data, List <PipelineException> pipelineExceptions
            )
            where TResponse : class, IElasticsearchResponse, new()
        {
            if (callDetails?.Success ?? false)
            {
                return(null);
            }

            var innerException = pipelineExceptions.HasAny() ? pipelineExceptions.AsAggregateOrFirst() : callDetails?.OriginalException;

            var statusCode = callDetails?.HttpStatusCode != null?callDetails.HttpStatusCode.Value.ToString() : "unknown";

            var resource = callDetails == null
                                ? "unknown resource"
                                : $"Status code {statusCode} from: {callDetails.HttpMethod} {callDetails.Uri.PathAndQuery}";


            var exceptionMessage = innerException?.Message ?? $"Request failed to execute";

            var pipelineFailure = data.OnFailurePipelineFailure;

            if (pipelineExceptions.HasAny())
            {
                pipelineFailure = pipelineExceptions.Last().FailureReason;
            }

            if (IsTakingTooLong)
            {
                pipelineFailure = PipelineFailure.MaxTimeoutReached;
                Audit(MaxTimeoutReached);
                exceptionMessage = "Maximum timeout reached while retrying request";
            }
            else if (Retried >= MaxRetries && MaxRetries > 0)
            {
                pipelineFailure = PipelineFailure.MaxRetriesReached;
                Audit(MaxRetriesReached);
                exceptionMessage = "Maximum number of retries reached";

                var now         = _dateTimeProvider.Now();
                var activeNodes = _connectionPool.Nodes.Count(n => n.IsAlive || n.DeadUntil <= now);
                if (Retried >= activeNodes)
                {
                    Audit(FailedOverAllNodes);
                    exceptionMessage += ", failed over to all the known alive nodes before failing";
                }
            }

            exceptionMessage += $". Call: {resource}";
            if (response != null && response.TryGetServerErrorReason(out var reason))
            {
                exceptionMessage += $". ServerError: {reason}";
            }

            var clientException = new ElasticsearchClientException(pipelineFailure, exceptionMessage, innerException)
            {
                Request    = data,
                Response   = callDetails,
                AuditTrail = AuditTrail
            };

            return(clientException);
        }