private ElasticsearchServerError ThrowOrGetErrorFromStreamResponse <T>(TransportRequestState <T> requestState, ElasticsearchResponse <Stream> streamResponse) { ElasticsearchServerError error = null; if ((!streamResponse.Success && requestState.RequestConfiguration == null) || (!streamResponse.Success && requestState.RequestConfiguration != null && requestState.RequestConfiguration.AllowedStatusCodes.All(i => i != streamResponse.HttpStatusCode))) { if (streamResponse.Response != null) { error = this.Serializer.Deserialize <ElasticsearchServerError>(streamResponse.Response); } else { error = new ElasticsearchServerError { Status = streamResponse.HttpStatusCode.GetValueOrDefault(-1) } }; if (this.Settings.ThrowOnElasticsearchServerExceptions) { throw new ElasticsearchServerException(error); } } return(error); }
//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; }
private void SetErrorDiagnosticsAndPatchSuccess <T>(TransportRequestState <T> requestState, ElasticsearchServerError error, ElasticsearchResponse <T> typedResponse, ElasticsearchResponse <Stream> 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; } }
//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); }
private ElasticsearchServerError ThrowOrGetErrorFromStreamResponse <T>( TransportRequestState <T> requestState, ElasticsearchResponse <Stream> streamResponse) { ElasticsearchServerError error = null; if ((!streamResponse.Success && requestState.RequestConfiguration == null) || (!streamResponse.Success && requestState.RequestConfiguration != null && requestState.RequestConfiguration.AllowedStatusCodes.All(i => i != streamResponse.HttpStatusCode))) { if (streamResponse.Response != null && !this.Settings.KeepRawResponse) { var e = this.Serializer.Deserialize <OneToOneServerException>(streamResponse.Response); error = ElasticsearchServerError.Create(e); } else if (streamResponse.Response != null && this.Settings.KeepRawResponse) { var ms = new MemoryStream(); streamResponse.Response.CopyTo(ms); ms.Position = 0; streamResponse.ResponseRaw = ms.ToArray(); var e = this.Serializer.Deserialize <OneToOneServerException>(ms); error = ElasticsearchServerError.Create(e); ms.Position = 0; streamResponse.Response = ms; } else { error = new ElasticsearchServerError { Status = streamResponse.HttpStatusCode.GetValueOrDefault(-1) } }; if (this.Settings.ThrowOnElasticsearchServerExceptions) { throw new ElasticsearchServerException(error); } } return(error); }
public ElasticsearchServerException(ElasticsearchServerError error) : base(ParseError(error)) { this.Status = error.Status; this.ExceptionType = error.ExceptionType; }
/* STREAM HANDLING ********************************************/ private ElasticsearchServerError ThrowOrGetErrorFromStreamResponse <T>( TransportRequestState <T> requestState, ElasticsearchResponse <Stream> streamResponse) { if (((streamResponse.HttpStatusCode.HasValue && streamResponse.HttpStatusCode.Value <= 0) || !streamResponse.HttpStatusCode.HasValue) && streamResponse.OriginalException != null) { throw streamResponse.OriginalException; } if (IsValidResponse(requestState, streamResponse)) { return(null); } 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) { 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); }