Skip to content

Commit f266bb8

Browse files
committed
Merge branch 'develop' into 1.x
2 parents e10914a + 0891445 commit f266bb8

File tree

4 files changed

+59
-6
lines changed

4 files changed

+59
-6
lines changed

src/Elasticsearch.Net/Connection/RequestHandlers/RequestHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ private ElasticsearchResponse<T> ReturnTypedResponse<T>(
109109
return this.StreamToTypedResponse<T>(streamResponse, requestState, bytes);
110110

111111
// If error read error
112-
error = GetErrorFromStream<T>(streamResponse.Response);
112+
error = GetErrorFromStream<T>(streamResponse.Response, streamResponse.HttpStatusCode ?? 0);
113113
var typedResponse = ElasticsearchResponse.CloneFrom<T>(streamResponse, default(T));
114114
this.SetStringOrByteResult(typedResponse, bytes);
115115
return typedResponse;

src/Elasticsearch.Net/Connection/RequestHandlers/RequestHandlerAsync.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ private Task<ReadResponse<T>> ReturnTypedResponse<T>(ElasticsearchResponse<Strea
131131
var typedResponse = ElasticsearchResponse.CloneFrom<T>(streamResponse, default(T));
132132
if (!isValidResponse)
133133
{
134-
response.Error = GetErrorFromStream<T>(gotStream.Result);
134+
response.Error = GetErrorFromStream<T>(gotStream.Result, streamResponse.HttpStatusCode ?? 0);
135135
this.SetStringOrByteResult(typedResponse, response.Bytes);
136136
if (gotStream.Result != null) gotStream.Result.Close();
137137
response.Response = typedResponse;

src/Elasticsearch.Net/Connection/RequestHandlers/RequestHandlerBase.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,20 @@ protected void SetByteResult(ElasticsearchResponse<byte[]> response, byte[] rawR
217217
response.Response = rawResponse;
218218
}
219219

220-
protected ElasticsearchServerError GetErrorFromStream<T>(Stream stream)
220+
protected ElasticsearchServerError GetErrorFromStream<T>(Stream stream, int httpStatusCode = 0)
221221
{
222222
try
223223
{
224224
var e = this._serializer.Deserialize<OneToOneServerException>(stream);
225+
if (e?.status == 0)
226+
{
227+
// Service unavailable isn't reported as a regular error, and
228+
// thus won't be serialized as such. This workaround improves
229+
// the error message for these errors.
230+
e.status = httpStatusCode;
231+
if (e.status == 503 && e.error.IsNullOrEmpty())
232+
e.error = "ServiceUnavailableException[Service Unavaliable. Try again later.]";
233+
}
225234
return ElasticsearchServerError.Create(e);
226235
}
227236
// ReSharper disable once EmptyGeneralCatchClause

src/Tests/Nest.Tests.Integration/Exceptions/ElasticsearchExceptionTests.cs

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
2+
using System.Diagnostics;
23
using System.Net;
3-
using System.Runtime.Remoting.Channels;
4+
using System.Threading.Tasks;
45
using Elasticsearch.Net;
56
using Elasticsearch.Net.ConnectionPool;
67
using Elasticsearch.Net.Exceptions;
@@ -179,7 +180,7 @@ public void ConnectionPool_DoesNotThrowOnServerExceptions_ThrowsMaxRetryExceptio
179180
}
180181

181182
[Test]
182-
public async void ConnectionPool_DoesNotThrowOnServerExceptions_ThrowsMaxRetryException_OnDeadNodes_Async()
183+
public async Task ConnectionPool_DoesNotThrowOnServerExceptions_ThrowsMaxRetryException_OnDeadNodes_Async()
183184
{
184185
var uris = new []
185186
{
@@ -232,7 +233,7 @@ public void ConnectionPool_ThrowOnServerExceptions_ThrowsElasticsearchServerExce
232233
}
233234

234235
[Test]
235-
public async void ConnectionPool_ThrowOnServerExceptions_ThrowsElasticsearchServerException_Async()
236+
public async Task ConnectionPool_ThrowOnServerExceptions_ThrowsElasticsearchServerException_Async()
236237
{
237238
var uris = new []
238239
{
@@ -262,5 +263,48 @@ public async void ConnectionPool_ThrowOnServerExceptions_ThrowsElasticsearchServ
262263
Assert.Fail("Did not expect exception of type {0} to be caught", e.GetType().Name);
263264
}
264265
}
266+
267+
[Test]
268+
// see https://github.com/elastic/elasticsearch/issues/9126 and https://github.com/elastic/elasticsearch-net/issues/1596
269+
public void ElasticsearchServerException_With_ServiceUnavailable_On_Response_NotValid_And_HttpStatusCode_503()
270+
{
271+
var uris = new[]
272+
{
273+
ElasticsearchConfiguration.CreateBaseUri(9200),
274+
ElasticsearchConfiguration.CreateBaseUri(9200),
275+
ElasticsearchConfiguration.CreateBaseUri(9200),
276+
};
277+
var connectionPool = new StaticConnectionPool(uris);
278+
var client = new ElasticClient(new ConnectionSettings(connectionPool)
279+
.ThrowOnElasticsearchServerExceptions()
280+
.SetTimeout(1000)
281+
);
282+
283+
var stopWatch = Stopwatch.StartNew();
284+
285+
try
286+
{
287+
var index = ElasticsearchConfiguration.NewUniqueIndexName();
288+
289+
while (stopWatch.Elapsed < TimeSpan.FromSeconds(5))
290+
{
291+
client.CreateIndex(index);
292+
client.Count(d => d.Index(index));
293+
client.DeleteIndex(index);
294+
}
295+
296+
Assert.Fail("Expected exception to be thrown");
297+
}
298+
catch (ElasticsearchServerException e)
299+
{
300+
e.Status.Should().Be(503);
301+
e.ExceptionType.Should().Contain("ServiceUnavailableException");
302+
e.Message.Should().Contain("Service Unavaliable. Try again later.");
303+
}
304+
catch (Exception e)
305+
{
306+
Assert.Fail("Did not expect exception of type {0} to be caught", e.GetType().Name);
307+
}
308+
}
265309
}
266310
}

0 commit comments

Comments
 (0)