Skip to content

Commit 5e8ff51

Browse files
committed
Extract 'RethrowKeepingStackTrace' to a separate class
Related to #1399
1 parent 8485bc7 commit 5e8ff51

File tree

3 files changed

+27
-19
lines changed

3 files changed

+27
-19
lines changed

src/Nest/ElasticClient.cs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5-
using System.Reflection;
65
using System.Threading.Tasks;
76
using Elasticsearch.Net;
87
using Elasticsearch.Net.Connection;
@@ -126,21 +125,6 @@ Func<D, D> selector
126125
return this.DispatchAsync<D, Q, R, I>(descriptor, dispatch);
127126
}
128127

129-
private static readonly Lazy<MethodInfo> preserveStackTraceMethodInfo = new Lazy<MethodInfo>(() =>
130-
typeof(Exception).GetMethod("InternalPreserveStackTrace", BindingFlags.Instance | BindingFlags.NonPublic)
131-
);
132-
133-
private static void RethrowKeepingStackTrace(Exception exception)
134-
{
135-
// In .Net 4.5 it would be simple : ExceptionDispatchInfo.Capture(exception).Throw();
136-
// But as NEST target .Net 4.0 the old internal method must be used
137-
if (preserveStackTraceMethodInfo.Value != null)
138-
{
139-
preserveStackTraceMethodInfo.Value.Invoke(exception, null);
140-
}
141-
throw exception;
142-
}
143-
144128
private Task<I> DispatchAsync<D, Q, R, I>(
145129
D descriptor
146130
, Func<ElasticsearchPathInfo<Q>, D, Task<ElasticsearchResponse<R>>> dispatch
@@ -158,13 +142,13 @@ D descriptor
158142
{
159143
var mr = r.Exception.InnerException as MaxRetryException;
160144
if (mr != null)
161-
RethrowKeepingStackTrace(mr);
145+
mr.RethrowKeepingStackTrace();
162146

163147
var ae = r.Exception.Flatten();
164148
if (ae.InnerException != null)
165-
RethrowKeepingStackTrace(ae.InnerException);
149+
ae.InnerException.RethrowKeepingStackTrace();
166150

167-
RethrowKeepingStackTrace(ae);
151+
ae.RethrowKeepingStackTrace();
168152
}
169153
return ResultsSelector<D, Q, R>(r.Result, descriptor);
170154
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Reflection;
3+
4+
namespace Elasticsearch.Net
5+
{
6+
internal static class ExceptionExtensions
7+
{
8+
private static readonly Lazy<MethodInfo> preserveStackTraceMethodInfo = new Lazy<MethodInfo>(() =>
9+
typeof(Exception).GetMethod("InternalPreserveStackTrace", BindingFlags.Instance | BindingFlags.NonPublic)
10+
);
11+
12+
public static void RethrowKeepingStackTrace(this Exception exception)
13+
{
14+
// In .Net 4.5 it would be simple : ExceptionDispatchInfo.Capture(exception).Throw();
15+
// But as NEST target .Net 4.0 the old internal method must be used
16+
if (preserveStackTraceMethodInfo.Value != null)
17+
{
18+
preserveStackTraceMethodInfo.Value.Invoke(exception, null);
19+
}
20+
throw exception;
21+
}
22+
}
23+
}

src/Nest/Nest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@
410410
<Compile Include="Exception\SnapshotException.cs" />
411411
<Compile Include="ExposedInternals\NestSerializer.cs" />
412412
<Compile Include="Enums\GeoDistance.cs" />
413+
<Compile Include="Extensions\ExceptionExtensions.cs" />
413414
<Compile Include="Obsolete\Obsolete.cs" />
414415
<Compile Include="Resolvers\Converters\Aggregations\FiltersAggregatorConverter.cs" />
415416
<Compile Include="Resolvers\Converters\Aggregations\FilterAggregatorConverter.cs" />

0 commit comments

Comments
 (0)