Skip to content

Commit 4d24081

Browse files
MpdreamzStuart Cam
authored andcommitted
net46 sets WebException as OriginalException where netcore sets Elast… (#3302)
`net46` sets `WebException` as `OriginalException` whereas netcore sets `ElasticsearchClientException`, because its `IConnection` throws and that `WebException` is now preserved with the backport of #3266
1 parent 8ce0a32 commit 4d24081

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/Elasticsearch.Net/Transport/Transport.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
using System.Threading;
44
using System;
55
using System.Linq;
6+
#if FEATURE_HTTPWEBREQUEST
7+
using System.Net;
8+
#endif
69

710
namespace Elasticsearch.Net
811
{
@@ -190,8 +193,22 @@ private static IApiCallDetails GetMostRecentCallDetails<TResponse>(TResponse res
190193

191194
private void HandleElasticsearchClientException(RequestData data, Exception clientException, IElasticsearchResponse response)
192195
{
193-
if (clientException != null && response.ApiCall.OriginalException == null && response.ApiCall is ApiCallDetails a)
194-
a.OriginalException = clientException;
196+
if (response.ApiCall is ApiCallDetails a)
197+
{
198+
//if original exception was not explicitly set during the pipeline
199+
//set it to the ElasticsearchClientException we created for the bad response
200+
if (clientException != null && a.OriginalException == null)
201+
a.OriginalException = clientException;
202+
//On .NET Core the IConnection implementation throws exceptions on bad responses
203+
//This causes it to behave differently to .NET FULL. We already wrapped the WebExeption
204+
//under ElasticsearchServerException and it exposes way more information as part of it's
205+
//exception message e.g the the root cause of the server error body.
206+
#if FEATURE_HTTPWEBREQUEST
207+
if (a.OriginalException is WebException)
208+
a.OriginalException = clientException;
209+
#endif
210+
}
211+
195212
this.Settings.OnRequestCompleted?.Invoke(response.ApiCall);
196213
if (clientException != null && data.ThrowExceptions) throw clientException;
197214
}

0 commit comments

Comments
 (0)