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);
 }
示例#2
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);
        }