Skip to content

Commit de25456

Browse files
committed
added support for all the different return types combinations on .Response and .ResponseRaw
1 parent 7357c0d commit de25456

File tree

8 files changed

+833
-138
lines changed

8 files changed

+833
-138
lines changed

src/Elasticsearch.Net/Connection/Transport.cs

Lines changed: 132 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,100 @@ private bool SniffingDisabled(IRequestConfiguration requestConfiguration)
152152
return requestConfiguration.SniffingDisabled.GetValueOrDefault(false);
153153
}
154154

155+
private bool SniffOnFaultDiscoveredMoreNodes<T>(TransportRequestState<T> requestState, int retried,
156+
ElasticsearchResponse<Stream> streamResponse)
157+
{
158+
if (retried != 0 || streamResponse.SuccessOrKnownError) return false;
159+
SniffOnConnectionFailure(requestState, retried);
160+
return this.GetMaximumRetries(requestState.RequestConfiguration) > 0;
161+
}
162+
163+
private void SetErrorDiagnosticsAndPatchSuccess<T>(TransportRequestState<T> requestState,
164+
ElasticsearchServerError error, ElasticsearchResponse<T> typedResponse, ElasticsearchResponse<Stream> streamResponse)
165+
{
166+
if (error != null)
167+
{
168+
typedResponse.Success = false;
169+
typedResponse.OriginalException = new ElasticsearchServerException(error);
170+
}
171+
if (!typedResponse.Success
172+
&& requestState.RequestConfiguration != null
173+
&& requestState.RequestConfiguration.AllowedStatusCodes.HasAny(i => i == streamResponse.HttpStatusCode))
174+
{
175+
typedResponse.Success = true;
176+
}
177+
}
178+
179+
180+
private Uri GetNextBaseUri<T>(TransportRequestState<T> requestState, out int initialSeed, out bool shouldPingHint)
181+
{
182+
if (requestState.RequestConfiguration != null && requestState.RequestConfiguration.ForcedNode != null)
183+
{
184+
initialSeed = 0;
185+
shouldPingHint = false;
186+
return requestState.RequestConfiguration.ForcedNode;
187+
}
188+
var baseUri = this._connectionPool.GetNext(requestState.Seed, out initialSeed, out shouldPingHint);
189+
return baseUri;
190+
}
191+
private Uri CreateUriToPath(Uri baseUri, string path)
192+
{
193+
var s = this.Settings;
194+
if (s.QueryStringParameters != null)
195+
{
196+
var tempUri = new Uri(baseUri, path);
197+
var qs = s.QueryStringParameters.ToQueryString(tempUri.Query.IsNullOrEmpty() ? "?" : "&");
198+
path += qs;
199+
}
200+
var uri = path.IsNullOrEmpty() ? baseUri : new Uri(baseUri, path);
201+
return uri.Purify();
202+
}
203+
204+
private byte[] PostData(object data)
205+
{
206+
var bytes = data as byte[];
207+
if (bytes != null)
208+
return bytes;
209+
210+
var s = data as string;
211+
if (s != null)
212+
return s.Utf8Bytes();
213+
if (data == null) return null;
214+
var ss = data as IEnumerable<string>;
215+
if (ss != null)
216+
return (string.Join("\n", ss) + "\n").Utf8Bytes();
217+
218+
var so = data as IEnumerable<object>;
219+
if (so == null)
220+
return this._serializer.Serialize(data);
221+
var joined = string.Join("\n", so
222+
.Select(soo => this._serializer.Serialize(soo, SerializationFormatting.None).Utf8String())) + "\n";
223+
return joined.Utf8Bytes();
224+
}
225+
226+
private void SetStringResult(ElasticsearchResponse<string> response, byte[] rawResponse)
227+
{
228+
response.Response = rawResponse.Utf8String();
229+
}
230+
231+
private void SetByteResult(ElasticsearchResponse<byte[]> response, byte[] rawResponse)
232+
{
233+
response.Response = rawResponse;
234+
}
235+
236+
private void SniffOnConnectionFailure<T>(TransportRequestState<T> requestState, int retried)
237+
{
238+
if (requestState.SniffedOnConnectionFailure
239+
|| SniffingDisabled(requestState.RequestConfiguration)
240+
|| !this.ConfigurationValues.SniffsOnConnectionFault
241+
|| retried != 0) return;
242+
243+
this.SniffClusterState();
244+
requestState.SniffedOnConnectionFailure = true;
245+
}
246+
155247
/* SYNC *** */
248+
156249
public ElasticsearchResponse<T> DoRequest<T>(string method, string path, object data = null, IRequestParameters requestParameters = null)
157250
{
158251
using (var tracer = new ElasticsearchResponseTracer<T>(this.Settings.TraceEnabled))
@@ -223,43 +316,6 @@ private ElasticsearchResponse<T> DoRequest<T>(TransportRequestState<T> requestSt
223316
return RetryRequest<T>(requestState, baseUri, retried);
224317
}
225318

226-
private bool SniffOnFaultDiscoveredMoreNodes<T>(TransportRequestState<T> requestState, int retried,
227-
ElasticsearchResponse<Stream> streamResponse)
228-
{
229-
if (retried != 0 || streamResponse.SuccessOrKnownError) return false;
230-
SniffOnConnectionFailure(requestState, retried);
231-
return this.GetMaximumRetries(requestState.RequestConfiguration) > 0;
232-
}
233-
234-
private void SetErrorDiagnosticsAndPatchSuccess<T>(TransportRequestState<T> requestState,
235-
ElasticsearchServerError error, ElasticsearchResponse<T> typedResponse, ElasticsearchResponse<Stream> streamResponse)
236-
{
237-
if (error != null)
238-
{
239-
typedResponse.Success = false;
240-
typedResponse.OriginalException = new ElasticsearchServerException(error);
241-
}
242-
if (!typedResponse.Success
243-
&& requestState.RequestConfiguration != null
244-
&& requestState.RequestConfiguration.AllowedStatusCodes.HasAny(i => i == streamResponse.HttpStatusCode))
245-
{
246-
typedResponse.Success = true;
247-
}
248-
}
249-
250-
251-
private Uri GetNextBaseUri<T>(TransportRequestState<T> requestState, out int initialSeed, out bool shouldPingHint)
252-
{
253-
if (requestState.RequestConfiguration != null && requestState.RequestConfiguration.ForcedNode != null)
254-
{
255-
initialSeed = 0;
256-
shouldPingHint = false;
257-
return requestState.RequestConfiguration.ForcedNode;
258-
}
259-
var baseUri = this._connectionPool.GetNext(requestState.Seed, out initialSeed, out shouldPingHint);
260-
return baseUri;
261-
}
262-
263319
private ElasticsearchResponse<T> RetryRequest<T>(TransportRequestState<T> requestState, Uri baseUri, int retried, Exception e = null)
264320
{
265321
var maxRetries = this.GetMaximumRetries(requestState.RequestConfiguration);
@@ -274,17 +330,6 @@ private ElasticsearchResponse<T> RetryRequest<T>(TransportRequestState<T> reques
274330
return this.DoRequest<T>(requestState, ++retried);
275331
}
276332

277-
private void SniffOnConnectionFailure<T>(TransportRequestState<T> requestState, int retried)
278-
{
279-
if (requestState.SniffedOnConnectionFailure
280-
|| SniffingDisabled(requestState.RequestConfiguration)
281-
|| !this.ConfigurationValues.SniffsOnConnectionFault
282-
|| retried != 0) return;
283-
284-
this.SniffClusterState();
285-
requestState.SniffedOnConnectionFailure = true;
286-
}
287-
288333
private ElasticsearchResponse<Stream> _doRequest(string method, Uri uri, byte[] postData, IRequestConfiguration requestSpecificConfig)
289334
{
290335
switch (method.ToLowerInvariant())
@@ -421,88 +466,54 @@ private Task<ElasticsearchResponse<Stream>> _doRequestAsync(string method, Uri u
421466
throw new Exception("Unknown HTTP method " + method);
422467
}
423468

424-
private Uri CreateUriToPath(Uri baseUri, string path)
425-
{
426-
var s = this.Settings;
427-
if (s.QueryStringParameters != null)
428-
{
429-
var tempUri = new Uri(baseUri, path);
430-
var qs = s.QueryStringParameters.ToQueryString(tempUri.Query.IsNullOrEmpty() ? "?" : "&");
431-
path += qs;
432-
}
433-
var uri = path.IsNullOrEmpty() ? baseUri : new Uri(baseUri, path);
434-
return uri.Purify();
435-
}
436-
437-
private byte[] PostData(object data)
438-
{
439-
var bytes = data as byte[];
440-
if (bytes != null)
441-
return bytes;
442-
443-
var s = data as string;
444-
if (s != null)
445-
return s.Utf8Bytes();
446-
if (data == null) return null;
447-
var ss = data as IEnumerable<string>;
448-
if (ss != null)
449-
return (string.Join("\n", ss) + "\n").Utf8Bytes();
450-
451-
var so = data as IEnumerable<object>;
452-
if (so == null)
453-
return this._serializer.Serialize(data);
454-
var joined = string.Join("\n", so
455-
.Select(soo => this._serializer.Serialize(soo, SerializationFormatting.None).Utf8String())) + "\n";
456-
return joined.Utf8Bytes();
457-
}
458-
459-
private void SetStringResult(ElasticsearchResponse<string> response, byte[] rawResponse)
460-
{
461-
response.Response = rawResponse.Utf8String();
462-
}
463-
464-
private void SetByteResult(ElasticsearchResponse<byte[]> response, byte[] rawResponse)
465-
{
466-
response.Response = rawResponse;
467-
}
468469

469470
private ElasticsearchServerError ThrowOrGetErrorFromStreamResponse<T>(
470471
TransportRequestState<T> requestState,
471472
ElasticsearchResponse<Stream> streamResponse)
472473
{
474+
if ((streamResponse.Success || requestState.RequestConfiguration != null) &&
475+
(streamResponse.Success || requestState.RequestConfiguration == null ||
476+
requestState.RequestConfiguration.AllowedStatusCodes.Any(i => i == streamResponse.HttpStatusCode)))
477+
return null;
478+
473479
ElasticsearchServerError error = null;
474-
if ((!streamResponse.Success && requestState.RequestConfiguration == null)
475-
|| (!streamResponse.Success
476-
&& requestState.RequestConfiguration != null
477-
&& requestState.RequestConfiguration.AllowedStatusCodes.All(i => i != streamResponse.HttpStatusCode)))
478-
{
480+
481+
var type = typeof(T);
482+
if (typeof(Stream).IsAssignableFrom(typeof(T)) || typeof(T) == typeof(VoidResponse))
483+
return null;
479484

480-
if (streamResponse.Response != null && !this.Settings.KeepRawResponse)
481-
{
482-
var e =this.Serializer.Deserialize<OneToOneServerException>(streamResponse.Response);
483-
error = ElasticsearchServerError.Create(e);
484-
}
485-
else if (streamResponse.Response != null && this.Settings.KeepRawResponse)
486-
{
487-
var ms = new MemoryStream();
488-
streamResponse.Response.CopyTo(ms);
489-
ms.Position = 0;
490-
streamResponse.ResponseRaw = ms.ToArray();
491-
var e =this.Serializer.Deserialize<OneToOneServerException>(ms);
492-
error = ElasticsearchServerError.Create(e);
493-
ms.Position = 0;
494-
streamResponse.Response = ms;
495-
}
496-
else
497-
error = new ElasticsearchServerError
498-
{
499-
Status = streamResponse.HttpStatusCode.GetValueOrDefault(-1)
500-
};
501-
if (this.Settings.ThrowOnElasticsearchServerExceptions)
502-
throw new ElasticsearchServerException(error);
485+
if (streamResponse.Response != null && !(this.Settings.KeepRawResponse || this.TypeOfResponseCopiesDirectly<T>()))
486+
{
487+
var e = this.Serializer.Deserialize<OneToOneServerException>(streamResponse.Response);
488+
error = ElasticsearchServerError.Create(e);
489+
}
490+
else if (streamResponse.Response != null)
491+
{
492+
var ms = new MemoryStream();
493+
streamResponse.Response.CopyTo(ms);
494+
ms.Position = 0;
495+
streamResponse.ResponseRaw = this.Settings.KeepRawResponse ? ms.ToArray() : null;
496+
var e =this.Serializer.Deserialize<OneToOneServerException>(ms);
497+
error = ElasticsearchServerError.Create(e);
498+
ms.Position = 0;
499+
streamResponse.Response = ms;
503500
}
501+
else
502+
error = new ElasticsearchServerError
503+
{
504+
Status = streamResponse.HttpStatusCode.GetValueOrDefault(-1)
505+
};
506+
if (this.Settings.ThrowOnElasticsearchServerExceptions)
507+
throw new ElasticsearchServerException(error);
504508
return error;
505509
}
510+
511+
private bool TypeOfResponseCopiesDirectly<T>()
512+
{
513+
var type = typeof(T);
514+
return type == typeof(string) || type == typeof(byte[]) || typeof(Stream).IsAssignableFrom(typeof(T));
515+
}
516+
506517
private ElasticsearchResponse<T> StreamToTypedResponse<T>(
507518
ElasticsearchResponse<Stream> streamResponse,
508519
object deserializationState)
@@ -519,7 +530,7 @@ private ElasticsearchResponse<T> StreamToTypedResponse<T>(
519530
return cs;
520531

521532
var type = typeof(T);
522-
if (!(this.Settings.KeepRawResponse || type == typeof(string) || type == typeof(byte[])))
533+
if (!(this.Settings.KeepRawResponse || this.TypeOfResponseCopiesDirectly<T>()))
523534
return this._deserializeToResponse(streamResponse.Response, deserializationState, cs);
524535

525536
if (streamResponse.Response != null)

src/Elasticsearch.Net/Domain/ElasticsearchResponse.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,24 @@ public class ElasticsearchResponse<T> : IElasticsearchResponse
7575

7676
public Exception OriginalException { get; protected internal set; }
7777

78+
/// <summary>
79+
/// This property returns the mapped elasticsearch server exception
80+
/// </summary>
81+
public ElasticsearchServerError ServerError
82+
{
83+
get
84+
{
85+
var esException = this.OriginalException as ElasticsearchServerException;
86+
if (esException == null) return null;
87+
return new ElasticsearchServerError
88+
{
89+
Error = esException.Message,
90+
ExceptionType = esException.ExceptionType,
91+
Status = esException.Status
92+
};
93+
}
94+
}
95+
7896
public string RequestMethod { get; protected internal set; }
7997

8098
public string RequestUrl { get; protected internal set; }

src/Nest/Domain/Responses/BaseResponse.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public interface IResponse : IResponseWithRequestInformation
1313
IElasticsearchResponse ConnectionStatus { get; }
1414
ElasticInferrer Infer { get; }
1515

16-
ElasticsearchServerError GetServerException();
16+
ElasticsearchServerError ServerError { get; }
1717
}
1818

1919
public class BaseResponse : IResponse
@@ -28,19 +28,22 @@ public BaseResponse()
2828

2929
public IElasticsearchResponse ConnectionStatus { get { return ((IResponseWithRequestInformation)this).RequestInformation; } }
3030

31-
public ElasticsearchServerError GetServerException()
31+
public ElasticsearchServerError ServerError
3232
{
33-
if (this.IsValid || this.ConnectionStatus == null || this.ConnectionStatus.OriginalException == null)
34-
return null;
35-
var e = this.ConnectionStatus.OriginalException as ElasticsearchServerException;
36-
if (e == null)
37-
return null;
38-
return new ElasticsearchServerError
33+
get
3934
{
40-
Status = e.Status,
41-
Error = e.Message,
42-
ExceptionType = e.ExceptionType
43-
};
35+
if (this.IsValid || this.ConnectionStatus == null || this.ConnectionStatus.OriginalException == null)
36+
return null;
37+
var e = this.ConnectionStatus.OriginalException as ElasticsearchServerException;
38+
if (e == null)
39+
return null;
40+
return new ElasticsearchServerError
41+
{
42+
Status = e.Status,
43+
Error = e.Message,
44+
ExceptionType = e.ExceptionType
45+
};
46+
}
4447
}
4548

4649
public ElasticInferrer _infer;

0 commit comments

Comments
 (0)