/// <summary> Try to parse an Elasticsearch <see cref="ServerError"/> </summary>
        public static bool TryGetElasticsearchServerError(this StringResponse response, out ServerError serverError)
        {
            serverError = null;
            if (string.IsNullOrEmpty(response.Body) || response.ResponseMimeType != RequestData.MimeType)
            {
                return(false);
            }

            var settings = response.ApiCall.ConnectionConfiguration;

            using var stream = settings.MemoryStreamFactory.Create(Encoding.UTF8.GetBytes(response.Body));
            return(ServerError.TryCreate(stream, out serverError));
        }
        /// <summary> Try to parse an Elasticsearch <see cref="ServerError"/> </summary>
        public static bool TryGetElasticsearchServerError(this BytesResponse response, out ServerError serverError)
        {
            serverError = null;
            if (response.Body == null || response.Body.Length == 0 || response.ResponseMimeType != RequestData.MimeType)
            {
                return(false);
            }

            var settings = response.ApiCall.ConnectionConfiguration;

            using var stream = settings.MemoryStreamFactory.Create(response.Body);
            return(ServerError.TryCreate(stream, out serverError));
        }
        /// <summary>
        /// Try to parse an Elasticsearch <see cref="ServerError"/>, this only works if
        /// <see cref="ITransportConfigurationValues.DisableDirectStreaming"/> gives us access to <see cref="IApiCallDetails.RequestBodyInBytes"/>
        /// </summary>
        public static bool TryGetElasticsearchServerError(this ITransportResponse response, out ServerError serverError)
        {
            serverError = null;
            var bytes = response.ApiCall.ResponseBodyInBytes;

            if (bytes == null || response.ApiCall.ResponseMimeType != RequestData.MimeType)
            {
                return(false);
            }

            var settings = response.ApiCall.ConnectionConfiguration;

            using var stream = settings.MemoryStreamFactory.Create(bytes);
            return(ServerError.TryCreate(stream, out serverError));
        }
Пример #4
0
        private static void RegisterError(ISpan span, IApiCallDetails response)
        {
            if (response.Success)
            {
                return;
            }

            var exception = response.OriginalException ?? response.AuditTrail.FirstOrDefault(a => a.Exception != null)?.Exception;
            var f         = PipelineFailure.Unexpected;

            // report inner exception stack traces for these directly if possible
            if (exception is ElasticsearchClientException es)
            {
                f         = es.FailureReason ?? f;
                exception = es.InnerException ?? es;
            }
            if (exception is UnexpectedElasticsearchClientException un)
            {
                f         = un.FailureReason ?? f;
                exception = un.InnerException ?? un;
            }

            var culprit = "Client Error";

            var message     = $"{f.GetStringValue()} {exception?.Message}";
            var stackFrames = exception == null ? null : new StackTrace(exception, true).GetFrames();

            if (stackFrames == null || stackFrames.Length == 0)
            {
                stackFrames = new StackTrace(true).GetFrames();
            }

            var causeOnServer = false;

            if (response.ResponseBodyInBytes != null)
            {
                using var memoryStream = new MemoryStream(response.ResponseBodyInBytes);
                if (ServerError.TryCreate(memoryStream, out var serverError) && serverError != null)
                {
                    causeOnServer = true;
                    culprit       = $"Elasticsearch Server Error: {serverError.Error.Type}";
                    message       = $"The server returned a ({response.HttpStatusCode}) and indicated: " + (
                        serverError.Error?.CausedBy?.Reason
                        ?? serverError.Error?.CausedBy?.Type
                        ?? serverError.Error?.RootCause.FirstOrDefault()?.Reason
                        ?? serverError.Error?.Reason
                        ?? "Response did not indicate a server error, usually means no json was with an error key was returned.");
                }
            }

            if (exception == null && !causeOnServer)
            {
                return;
            }
            if (causeOnServer && string.IsNullOrEmpty(message))
            {
                return;
            }

            if (causeOnServer)
            {
                span.CaptureError(message, culprit, stackFrames);
            }
            else
            {
                span.CaptureException(exception);
            }
        }