protected void OptionallyCloseResponseStreamAndSetSuccess <T>(
            ITransportRequestState requestState,
            ElasticsearchServerError error,
            ElasticsearchResponse <T> typedResponse,
            ElasticsearchResponse <Stream> streamResponse)
        {
            if (streamResponse.Response != null && !typeof(Stream).IsAssignableFrom(typeof(T)))
            {
                streamResponse.Response.Close();
            }

            if (error != null)
            {
                typedResponse.Success = false;
                if (typedResponse.OriginalException == null)
                {
                    typedResponse.OriginalException = new ElasticsearchServerException(error);
                }
            }

            //TODO UNIT TEST OR BEGONE
            if (!typedResponse.Success &&
                requestState.RequestConfiguration != null &&
                requestState.RequestConfiguration.AllowedStatusCodes.HasAny(i => i == streamResponse.HttpStatusCode))
            {
                typedResponse.Success = true;
            }
        }
Exemple #2
0
        private ElasticsearchResponse <T> CoordinateRequest <T>(TransportRequestState <T> requestState, int maxRetries, int retried, ref bool aliveResponse)
        {
            var pingRetryRequest = this.SelectNextNode(requestState);

            if (pingRetryRequest != null)
            {
                return(pingRetryRequest);
            }

            var streamResponse = this.DoElasticsearchCall(requestState);

            aliveResponse = streamResponse.SuccessOrKnownError;

            if (!this.DoneProcessing(streamResponse, requestState, maxRetries, retried))
            {
                return(null);
            }

            ElasticsearchServerError error = null;
            var typedResponse = this.ReturnStreamOrVoidResponse(requestState, streamResponse)
                                ?? this.ReturnTypedResponse(requestState, streamResponse, out error);

            this.OptionallyCloseResponseStreamAndSetSuccess(requestState, error, typedResponse, streamResponse);
            if (error != null && this._settings.ThrowOnElasticsearchServerExceptions)
            {
                throw new ElasticsearchServerException(error);
            }
            return(typedResponse);
        }
 protected ElasticsearchServerError GetErrorFromStream <T>(Stream stream)
 {
     try
     {
         var e = this._serializer.Deserialize <OneToOneServerException>(stream);
         return(ElasticsearchServerError.Create(e));
     }
     // ReSharper disable once EmptyGeneralCatchClause
     // parsing failure of exception should not be fatal, its a best case helper.
     catch { }
     return(null);
 }
Exemple #4
0
 private void SetErrorDiagnosticsAndPatchSuccess <T>(
     ITransportRequestState requestState,
     ElasticsearchServerError error,
     ElasticsearchResponse <T> typedResponse,
     IElasticsearchResponse streamResponse)
 {
     if (error != null)
     {
         typedResponse.Success           = false;
         typedResponse.OriginalException = new ElasticsearchServerException(error);
     }
     if (!typedResponse.Success &&
         requestState.RequestConfiguration != null &&
         requestState.RequestConfiguration.AllowedStatusCodes.HasAny(i => i == streamResponse.HttpStatusCode))
     {
         typedResponse.Success = true;
     }
 }
 public EsDataPersistanceException(string action, IResponse response) :
     base(string.Format(
             "Elasticsearch error occured when {0} : \n\n{1}", 
             action, 
             string.Format(
                 "({0}) {1} - {2}\n---------------------------------------------\n{3}\n{4}\n---------------------------------------------\n{5}", 
                 response.RequestInformation.HttpStatusCode,
                 response.RequestInformation.RequestMethod,
                 response.RequestInformation.RequestUrl,
                 response.ServerError.Error,
                 response.ServerError.ExceptionType,
                 response.RequestInformation.ResponseRaw == null ?
                     string.Empty :
                     System.Text.Encoding.Default.GetString(response.RequestInformation.ResponseRaw))))
 {
     this.error = response.ServerError;
     this.requestInfo = response.RequestInformation;
 }
        //iffy side effect assignment to exceptionType needed so that we simply return message to the
        //base constructor.
        private static string ParseError(ElasticsearchServerError error)
        {
            if (error == null)
            {
                return(_couldNotParseServerException);
            }
            if (error.Error.IsNullOrEmpty())
            {
                return(_couldNotParseServerException);
            }
            var matches = ExceptionSplitter.Match(error.Error);

            if (matches.Groups.Count != 3)
            {
                return(_couldNotParseServerException);
            }

            error.ExceptionType = matches.Groups[1].Value;
            return(matches.Groups[2].Value);
        }
Exemple #7
0
        private ElasticsearchResponse <T> ReturnTypedResponse <T>(
            TransportRequestState <T> requestState,
            ElasticsearchResponse <Stream> streamResponse,
            out ElasticsearchServerError error)
        {
            error = null;

            // Read to memory stream if needed
            var hasResponse = streamResponse.Response != null;
            var forceRead   = this._settings.KeepRawResponse || typeof(T) == typeof(string) || typeof(T) == typeof(byte[]);

            byte[] bytes = null;
            if (hasResponse && forceRead)
            {
                var ms = this._memoryStreamProvider.New();
                streamResponse.Response.CopyTo(ms);
                bytes = ms.ToArray();
                streamResponse.Response.Close();
                streamResponse.Response          = ms;
                streamResponse.Response.Position = 0;
            }
            // Set rawresponse if needed
            if (this._settings.KeepRawResponse)
            {
                streamResponse.ResponseRaw = bytes;
            }

            var isValidResponse = IsValidResponse(requestState, streamResponse);

            if (isValidResponse)
            {
                return(this.StreamToTypedResponse <T>(streamResponse, requestState, bytes));
            }

            // If error read error
            error = GetErrorFromStream <T>(streamResponse.Response);
            var typedResponse = ElasticsearchResponse.CloneFrom <T>(streamResponse, default(T));

            this.SetStringOrByteResult(typedResponse, bytes);
            return(typedResponse);
        }
 public ElasticsearchException(ElasticsearchServerError error) : this(error.Error, error.ExceptionType, error.Status)
 {
 }
 public ElasticsearchServerException(ElasticsearchServerError error) : base(ParseError(error))
 {
     this.Status        = error.Status;
     this.ExceptionType = error.ExceptionType;
 }
Exemple #10
0
        /* STREAM HANDLING	********************************************/

        private ElasticsearchServerError ThrowOrGetErrorFromStreamResponse <T>(
            TransportRequestState <T> requestState,
            ElasticsearchResponse <Stream> streamResponse)
        {
            if (IsValidResponse(requestState, streamResponse))
            {
                return(null);
            }

            if (((streamResponse.HttpStatusCode.HasValue && streamResponse.HttpStatusCode.Value <= 0) ||
                 !streamResponse.HttpStatusCode.HasValue) && streamResponse.OriginalException != null)
            {
                throw streamResponse.OriginalException;
            }

            ElasticsearchServerError error = null;

            var type = typeof(T);

            if (typeof(Stream).IsAssignableFrom(typeof(T)) || typeof(T) == typeof(VoidResponse))
            {
                return(null);
            }

            if (streamResponse.Response != null && !(this.Settings.KeepRawResponse || this.TypeOfResponseCopiesDirectly <T>()))
            {
                var e = this.Serializer.Deserialize <OneToOneServerException>(streamResponse.Response);
                error = ElasticsearchServerError.Create(e);
            }
            else if (streamResponse.Response != null)
            {
                var ms = new MemoryStream();
                streamResponse.Response.CopyTo(ms);
                ms.Position = 0;
                streamResponse.ResponseRaw = this.Settings.KeepRawResponse ? ms.ToArray() : null;
                try
                {
                    var e = this.Serializer.Deserialize <OneToOneServerException>(ms);
                    error = ElasticsearchServerError.Create(e);
                }
                catch (Exception e)
                {
                    var raw = ms.ToArray().Utf8String();
                }
                ms.Position = 0;
                streamResponse.Response.Close();
                streamResponse.Response = ms;
            }
            else
            {
                error = new ElasticsearchServerError
                {
                    Status = streamResponse.HttpStatusCode.GetValueOrDefault(-1)
                }
            };
            if (this.Settings.ThrowOnElasticsearchServerExceptions)
            {
                throw new ElasticsearchServerException(error);
            }
            return(error);
        }
 public EsDataPersistanceException(string message) : base(message)
 {
     this.error = null;
 }
 public ElasticClientQueryObjectException(ElasticsearchServerError error) : base(error)
 {
 }